Imports System
Imports System.Configuration
Imports MyClasses
Imports DAL
Imports System.Text
Imports BAL
Imports System.Data.SqlClient

Public Class FrmChallanUpdate

#Region "Declaration"


    Private intTotalColumns As Integer = 23      'Sale Item's no of Columns    

    Private btStatus As Byte                  'Current Record Status 
    Private intGrdCntRows As Integer = 1
    'Private intCrntItmCnt As Integer             'Used to show No of Active Items  in Current Counter
    Private BtLoading As Boolean                    'Variable is used to avoid unNecessary loops/events/execution while setting or Loading the Data                     

    Private intParty As Integer = 0           'Variable stores currently selected party 
    Private intPromoter As Integer = 0           'Variable stores currently selected promoter
    'Private intColor As Integer = 0
    'Private IntItem As Integer = 0
    Private intPriceList As Integer = 0
    Private intSite As Integer = 0
    'Private intGodownID As Integer = 0
    ' Private intTaxID As Integer = 0

    'Private intPackSizeID As Integer = 0
    ' Private intPackUpdateDtl_PK As Integer = 0
    ' Private intPackingID As Integer = 0


    Private saleType As DGeneral.DocTypes
    Private intVoucherID As Integer
    Dim dtbTaxes As DataTable
    Private hstTAxes As Hashtable
    Private dtbPacking As DataTable
    Private TaxCode As String
    Dim strSeqID As String
    Dim intBlankID As Integer
    Structure StructTaxDetails
        Dim intTax As Integer
        Dim StrTaxValue As Double
        Dim StrTotalTaxableAmt As Double
        Dim strTotalTaxApplied As Double
    End Structure
#End Region '"DECLARATION"

#Region " Form( & control) Events"
    Private Sub txtAmt_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        e.SuppressKeyPress = True  'INSTEAD OF DISABLE CONTROL(IT AFFECTS THE UI) MANUAY KEYS ARE SUPPRESSED SO USER CANT EDIT  THE VALUE. TEXT BOX LOOKS ENABLE BUT NOT
    End Sub

    Private Sub txtReturn_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        e.SuppressKeyPress = True 'INSTEAD OF DISABLE CONTROL(IT AFFECTS THE UI) MANUAY KEYS ARE SUPPRESSED SO USER CANT EDIT  THE VALUE. TEXT BOX LOOKS ENABLE BUT NOT
    End Sub

    Private Sub txtPaidAmt_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Try
            If e.KeyCode = Keys.Enter Then
                btnSave.Focus()
            Else
                If e.Alt = True Then Exit Sub
                If BCommon.ValidateText(txtOtherCharges.Text, e.KeyCode, "NUM", False) = False Then
                    e.SuppressKeyPress = True
                ElseIf e.Shift Then
                    e.SuppressKeyPress = True : Exit Sub
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "txtPaidAmt_KeyDown")
        End Try
    End Sub

    Private Sub txtPaidAmt_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Try
            If e.KeyCode = Keys.Enter Then
                If txtOtherCharges.Text >= txtTotalAmt.Text Then
                    txtGrossAmt.Text = Format((txtOtherCharges.Text - txtTotalAmt.Text), "##,##0.00")
                Else : txtGrossAmt.Text = "0.00"  'NOW THIS FORM IS FOR COUNTER SALE ONLY SO THERE IS NO VALIDATION LIKE THIS 'CHANDRAKANT
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "txtPaidAmt_KeyUp")
        End Try
    End Sub

    Private Sub frmSale_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
        Try
            'If DSale.GetCounterDetails().Rows.Count > 0 Then
            '    'Select Case MessageBox.Show(BCommon.GetMessage(10327), BCommon.GetConfigValues("MsgTitle"), MessageBoxButtons.YesNoCancel)
            '    Select Case BCommon.ShowMsgError(10327, "YNC")
            '        Case Windows.Forms.DialogResult.Yes      'SAVE All COUNTER DATA & CLOSE.
            '            'save data
            '            BCommon.ShowMsgError(3)
            '        Case Windows.Forms.DialogResult.No       'CLOSE THE FORM
            '            'NO NEED TO CODE
            '        Case Windows.Forms.DialogResult.Cancel   'CANCEL THE FORM CLOSE EVENT
            '            e.Cancel = True
            '    End Select
            'End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "frmSale_FormClosing")
        End Try
    End Sub

    Private Sub FrmApprovalUpdate_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Try
            BtLoading = True
            BCommon.SetFormStandards(Me)
            LoadTaxes()
            LoadPacking()
            Call DisplayData()
            txtPartyCode.Focus()


            BtLoading = False

        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "FrmApprovalUpdate_Load")
        Finally
            SubPriSetSeqID()
            ' saleType = DGeneral.DocTypes.APPROVAL_UPDATE
        End Try
        SubPriSelectDate()
    End Sub
#End Region

#Region "BUTTON EVENTS"

    Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
        ClearData()
    End Sub
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Try
            ' If btStatus = 1 Then 'Current Record is Updated 
            Me.Close()   'Current Record is not updated
        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "btnClose_Click")
        End Try
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            '    If btStatus = 1 Then SaveCurrentItem(intCurrentRow)
            If ValidateInfo() = True Then
                If SaveData() = 1 Then
                    Call ClearData()
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "btnSaveCounter_Click")
        End Try
    End Sub
    Private Sub SubPriSetSeqID()

        If rdbCashMemo.Checked Then
            saleType = DGeneral.DocTypes.CASH_MEMO
        Else
            saleType = DGeneral.DocTypes.CHALLAN
        End If
        txtVoucher.Text = BCommon.GetSequentialID(saleType)
        'If saleType = DGeneral.DocTypes.INVOICE Then
        '    txtVoucher.Text = BCommon.GetSequentialID(DGeneral.DocTypes.INVOICE)
        'Else
        '    txtVoucher.Text = BCommon.GetSequentialID(saleType)
        'End If
        'txtVoucher.Text = BCommon.GetSequentialID(saleType)
    End Sub
#End Region  '"BUTTON EVENTS"


    Private Sub DisplayData()
        Try
            BtLoading = True

            FillPriceListCombo()
            SubPriSetTaxGrid()


            BCommon.SetTransationDate(dtpSaleDate)
            'dtpSaleDate.Enabled = False

            'BtnCash_Click(Nothing, Nothing)
            SubPriSetSeqID()

            'Call SetGrids()
            Call SetOtherCharges()
            ' Call SetColumnWidths()
            'Call SetTabIndexes()


            BtLoading = False

            BCommon.SetTextReadOnly(txtVoucher)
        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "DisplayData")
        End Try
    End Sub

    Private Sub ClearData()
        Try

            BtLoading = True

            btStatus = 0

            intVoucherID = 0
            txtGrossAmt.Text = "0.00"
            txtTotalAmt.Text = "0.00"
            txtOtherCharges.Text = "0.00"

            'subPriClearItemDtls()
            SetOtherCharges()
            ClearTaxes()
            LoadTaxes()
            intPromoter = 0
            intParty = 0
            lblChallanType.Text = ""
            intSite = 0
            intPriceList = 0

            txtPartyCode.Text = String.Empty
            txtPartyName.Text = String.Empty
            txtPromoterCode.Text = String.Empty
            txtPromoterName.Text = String.Empty
            txtSiteCode.Text = String.Empty
            txtSiteName.Text = String.Empty
            rTxtAddress.Text = String.Empty
            SrchBtnClear_Click(Nothing, Nothing)
            fgApprovalItems.Rows.Clear()

            txtSearchedMinusSelected.Text = "0.00"
            txtSearchedTotal.Text = "0.00"
            txtSelectedTotal.Text = "0.00"
            txtDateTotal.Text = "0.00"
            txtUnSelected.Text = "0.00"
            SubPriSetSeqID()
            'CmbPriceList.SelectedIndex = -1

            'With fgItems
            '    .Rows.Clear()
            '    .Rows.Add(intGrdCntRows)
            '    .Refresh()
            'End With
            'Call BtnCash_Click(Nothing, Nothing)
            BtLoading = False
        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "ClearData")
        End Try
    End Sub
    Private Function SaveData() As Byte
        Try

            Dim objsaleItem() As CNGItem = SetSaleItemDetails()
            If objsaleItem.Length = 0 Then
                MessageBox.Show("Kindly select at least one product!")
                Return 0
            End If
            Dim objsale As CSale = SetSaleData()
            Dim objdSale As DSale = New DSale()
            Dim StrSeqId As String = objdSale.Save(objsale, objsaleItem, dgOtherCharges.DataSource, dgTaxes.DataSource, intVoucherID, True)

            If StrSeqId <> String.Empty AndAlso StrSeqId <> "0" Then
                If saleType <> DGeneral.DocTypes.CHALLAN AndAlso saleType <> DGeneral.DocTypes.CHALLAN Then
                    If MessageBox.Show("Do you want to print the voucher :- " & StrSeqId.ToString, "NGS", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                        objdSale.updateBilledStatus()
                        '''''''''''''''''''''''''PRINTTING FRUNCTIONS NEDD TO BE CALL'''''''''''''''''''''''''''''''''''''''''''''''
                        '''''''''''''''''''''''''PRINTTING FRUNCTIONS NEDD TO BE CALL'''''''''''''''''''''''''''''''''''''''''''''''
                    End If
                End If
                Return 1
            Else
                Return 0
            End If

        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "SaveData")
            Return 0
        End Try
    End Function

    Private Function SetSaleData() As CSale
        Dim objsale As New CSale
        'Dim intCount As Integer
        Dim TotAmount As Double
        'Dim TotDisc As Double
        Try
            With objsale
                ' .PartyID = CustID
                .SaleDate = Me.dtpSaleDate.Value
                .IsFinalBill = True
                'For intCount = 1 To fgItems.Rows.Count
                '    If Not IsNothing(fgItems.Columns("ITEMID").Item(intCount)) Then
                '        TotAmount = TotAmount + fgItems.Columns("AMOUNT").Item(intCount)
                '    Else : Exit For
                '    End If
                'Next
                .SaleBillID = intVoucherID
                .SaleType = saleType
                TotAmount = System.Convert.ToDouble(txtTotalAmt.Text.Trim())
                .Amount = TotAmount
                .dblOtherCharges = System.Convert.ToDouble(txtOtherCharges.Text.Trim())
                .Discount = System.Convert.ToDouble(txtTotalDisc.Text.Trim())
                .TotalTax = 0

                .GrossAmt = System.Convert.ToDouble(txtGrossAmt.Text.Trim())
                .PartyID = intParty 'cmbPartyCode.SelectedValue
                .PromoterID = intPromoter 'CmbPromoterCode.SelectedValue
                .Site = intSite
                .priceListID = intPriceList
                .ShippingAdress = rTxtAddress.Text.Trim()
            End With
            Return objsale
        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "SetSaleData")
            Return Nothing
        End Try
    End Function

    Private Function SetSaleItemDetails() As CNGItem()
        Dim objsaleItem() As CNGItem
        Dim intCount As Integer
        Dim intRowCount As Integer = 0
        Try
            For intCount = 0 To fgApprovalItems.Rows.Count - 1

                If fgApprovalItems.Item(SarchAprvlColumns.ItemID, intCount).Value = Nothing OrElse Trim(fgApprovalItems.Item(SarchAprvlColumns.ItemID, intCount).Value.ToString) = "" Then
                    Exit For
                ElseIf fgApprovalItems.Item(SarchAprvlColumns.SelectItem, intCount).Value = True Then
                    ReDim objsaleItem(intRowCount - 1)
                    ' objsaleItem(intRowCount - 1) =
                    intRowCount = intRowCount + 1
                End If
            Next
            ReDim objsaleItem(intRowCount - 1)
            Dim iSelectedItem As Integer = 0
            For intCount = 0 To fgApprovalItems.Rows.Count - 1
                If fgApprovalItems.Item(SarchAprvlColumns.SelectItem, intCount).Value = True Then
                    objsaleItem(iSelectedItem) = New CNGItem
                    objsaleItem(iSelectedItem) = SetSaleItemDetail(iSelectedItem)
                    iSelectedItem += 1
                End If

            Next
            Return objsaleItem
        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "SetSaleItemDetails")
            Return Nothing
        End Try
    End Function

    Private Function SetSaleItemDetail(ByVal intRow As Integer) As CNGItem
        Dim Item As CNGItem = New CNGItem
        Try
            With Item
                .iBillID = fgApprovalItems.Item(SarchAprvlColumns.SaleID, intRow).Value
                .intPackSizeID = fgApprovalItems.Item(SarchAprvlColumns.PACK_SIZE_ID, intRow).Value
                .ColorCode = fgApprovalItems.Item(SarchAprvlColumns.CLRID, intRow).Value
                .ItemID = fgApprovalItems.Item(SarchAprvlColumns.ItemID, intRow).Value
                .Quantity = fgApprovalItems.Item(SarchAprvlColumns.Qty, intRow).Value
                .Amount = fgApprovalItems.Item(SarchAprvlColumns.Amount, intRow).Value
                .dblRate = fgApprovalItems.Item(SarchAprvlColumns.SalePrice, intRow).Value
                .intPackUpdateDtl_PK = fgApprovalItems.Item(SarchAprvlColumns.PackUpdateID, intRow).Value
                '.dblDISCAmt = fgApprovalItems.Item(SarchAprvlColumns.DISC_AMT, intRow).Value
                .strDisc = "0" ' fgApprovalItems.Item(SarchAprvlColumns.Disc, intRow).Value

                .TaxID = fgApprovalItems.Item(SarchAprvlColumns.TaxID, intRow).Value
                .TaxAmt = fgApprovalItems.Item(SarchAprvlColumns.TAXAMT, intRow).Value

            End With

            Return Item
        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "SetSaleItemDetail")
            Return Nothing
        End Try
    End Function

    Private Function ValidateInfo() As Boolean
        '
        Try
            If saleType = DGeneral.DocTypes.CHALLAN OrElse saleType = DGeneral.DocTypes.INVOICE Then
                If intParty = 0 Then
                    MessageBox.Show("Please select Party!!", "NGS", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                    txtPartyCode.Focus()
                    Return False
                End If
            End If
            Dim intPromoter As Integer = -1
            With fgApprovalItems

                For intCount As Integer = 0 To .Rows.Count - 1
                    If .Item(SarchAprvlColumns.SelectItem, intCount).Value = True Then
                        If (intPromoter < 0) Then
                            intPromoter = .Item(SarchAprvlColumns.SelectItem, intCount).Value
                        ElseIf intPromoter <> .Item(SarchAprvlColumns.PromoterCode, intCount).Value Then
                            MessageBox.Show("SELECTED ITEMS ARE OF MULTIPLE PROMOTERS!", "NGS", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                            Return False
                        End If
                    End If
                Next
            End With

            Return True
        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "ValidateInfo")
        End Try
    End Function

    Private Sub FillPriceListCombo()
        Dim dtbPriceList As DataTable = DSale.GetPriceList()
        'CmbPriceList.DataSource = dtbPriceList
        'CmbPriceList.DisplayMember = "PRICELISTNAME"
        'CmbPriceList.ValueMember = "ID"
        'CmbPriceList.SelectedIndex = -1
        'intPriceList = CmbPriceList.SelectedValue
    End Sub

#Region "ONLY FOR SALE FORM"


    Private Sub CalculateAmt()
        Dim intCount As Integer
        Dim TotAmount As Double = 0      '
        Dim TotalDiscAmt As Double = 0
        Try
            With fgApprovalItems

                ClearTaxes()
                ClearSummary()
                For intCount = 0 To .Rows.Count - 1
                    If .Item(SarchAprvlColumns.SelectItem, intCount).Value = True Then

                        If IsNothing(.Item(SarchAprvlColumns.Amount, intCount).Value) OrElse Trim(.Item(SarchAprvlColumns.Amount, intCount).Value) = "" Then Exit For
                        TotAmount = TotAmount + .Item(SarchAprvlColumns.Amount, intCount).Value
                        'TotalDiscAmt = TotalDiscAmt + .Item(SarchAprvlColumns.DISC_AMT, intCount).Value
                        AddTaxes(.Item(SarchAprvlColumns.TaxID, intCount).Value, .Item(SarchAprvlColumns.TAXAMT, intCount).Value, .Item(SarchAprvlColumns.Amount, intCount).Value)
                        AddToSummary(.Item(SarchAprvlColumns.PACKINGID, intCount).Value, .Item(SarchAprvlColumns.Qty, intCount).Value)
                    End If

                Next
                txtTotalAmt.Text = Format(TotAmount, "0.00")
                txtTotalDisc.Text = Format(TotalDiscAmt, "0.00")
                CalculateTotalOtherCharges()
                SubPriUpdateAndDisplaySumary()
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "CalculateAmt")
        End Try
    End Sub
#End Region '"ONLY FOR SALE FORM"


    'Enum ItemGridColumns
    '    ITEMID = 0
    '    CODE = 1
    '    ITEMNAME = 2

    '    CLRCODE = 3
    '    CLRDESC = 4

    '    PACK = 5
    '    GODOWN = 6
    '    'SCHEMEID = 7
    '    'DISC = 8
    '    QTY = 7
    '    RATE = 8

    '    DISC = 9
    '    DISC_AMT = 10

    '    AMOUNT = 11
    '    STATUS = 12

    '    CLRID = 13
    '    GODOWNID = 14

    '    STOCKDETAILS = 15
    '    TAXID = 16
    '    PACK_SIZE_ID = 17
    '    PACKUPDATEID_PK = 18
    '    TAXCODE = 19
    '    PACKINGID = 20
    '    TAXAMT = 21
    '    IsTINTIN = 22
    'End Enum
#Region "COMMON SEARCH"


    Private Sub txtPartyCode_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPartyCode.Enter, txtPartyName.Enter, txtPromoterCode.Enter, txtPromoterName.Enter, txtSiteCode.Enter, txtSiteName.Enter
        If BtLoading Then Exit Sub
        If sender.Text.ToString = String.Empty Then
            sender.BackColor = Color.LightBlue
            SearchParty_Promoter_Site(sender)
        End If
    End Sub
    Private Sub txtCLRCODE_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPartyCode.KeyDown, txtPartyName.KeyDown, txtPromoterCode.KeyDown, txtPromoterName.KeyDown, txtSiteCode.KeyDown, txtSiteName.KeyDown
        If e.KeyCode = Keys.Back Then
            e.SuppressKeyPress = True
            SearchParty_Promoter_Site(sender)
        ElseIf e.KeyCode = Keys.Enter Then
            SendKeys.Send("{Tab}")
        ElseIf e.KeyCode = Keys.Tab Then

        End If
    End Sub

    Private Function GetSearchQuery(ByVal Sender As Object, ByRef strSearchString As String) As String
        Select Case Sender.Name
            Case txtPartyCode.Name, txtPartyName.Name
                strSearchString = "PARTY "
                Return "SELECT P.Id,P.PartyCode AS CODE ,P.PartyName  AS NAME, P.Address +', PIN:' + CONVERT(VARCHAR (6),P.PinCode) +', ' + C.CityName  AS PartyAddrs,ISNULL(PriceListId,0) AS PriceListId,P.ChallanType  FROM [NGSPaintsDB].[dbo].Party P INNER JOIN City	C ON (P.CityID= C.ID) WHERE P.IsDeleted=0 " & IIf(saleType = DGeneral.DocTypes.INVOICE, " AND P.ChallanType='C'", "")
            Case txtPromoterCode.Name, txtPromoterName.Name
                strSearchString = "PROMOTER "
                Return "SELECT P.Id,P.PromoterCode AS CODE ,P.PromoterName AS NAME FROM [NGSPaintsDB].[dbo].promoter P WHERE P.IsDeleted=0"
            Case txtSiteCode.Name, txtSiteName.Name
                strSearchString = "SITE "
                Return " SELECT [Id],[SiteCode]AS CODE ,[SiteName] AS NAME,ISNULL(PriceListId,0) AS PriceListId  FROM [NGSPaintsDB].[dbo].[Site] WHERE [IsDeleted]=0" & IIf(intParty > 0, " AND PartyID=" & intParty.ToString, "")
                'Case txtPrice.Name 'PRICE LIST
                'CmbPriceList.Focus()    Return "SELECT [ID],[PRICELISTCODE]AS CODE ,[PRICELISTNAME] AS NAME  FROM [NGSPAINTSDB].[DBO].[PRICELIST] WHERE [ISDELETED]=0"
                'Case txtItemCode.Name, txtItemName.Name
                '    strSearchString = "PRODUCT "
                '    Return "SELECT P.[Id] AS ItemID,P.[ProductCode] AS Code,P.[ProductName] AS Name, P.TAXID FROM [NGSPaintsDB].[dbo].Product P WHERE ISDeleted=0 ORDER BY NAME"
                'Case txtCLRCODE.Name, txtCLRNAME.Name
                '    If IntItem = 0 Then
                '        Return ""
                '    End If
                '    strSearchString = "COLOR "
                '    Return "SELECT DISTINCT C.[Id],C.[ColorCode] AS CODE ,C.[ColorDescription] AS NAME  FROM [Color] AS C  INNER JOIN  PackingUpdateDetail  D ON (D.ColorID=C.ID) INNER JOIN  PackingUpdate P ON(P.ID=D.PackingUpdateId) WHERE C.[IsDeleted]=0 AND P.ProductID=" & IntItem.ToString
                'Case txtGoDown.Name
                '    If IntItem = 0 OrElse intColor = 0 OrElse intPackSizeID = 0 Then
                '        Return ""
                '    End If
                '    strSearchString = "GODOWN "
                '    Return "SELECT DISTINCT (C.[Id]),C.[GoDownCode] AS CODE ,C.[GodownName] AS NAME ,Stock, D.ID AS PackUpdateID,D.PackingID FROM PackingUpdateDetail D INNER JOIN [GoDown] C ON (D.GoDownID=C.ID AND D.ColorID=" & intColor & ")  INNER JOIN  PackingUpdate P ON(P.ID=D.PackingUpdateId AND P.ProductID=" & IntItem & ")  WHERE Stock > 0 AND D.PackSizeID=" & intPackSizeID.ToString()
                'Case cmbPack.Name
                '    If IntItem = 0 OrElse intColor = 0 Then
                '        Return ""
                '    End If
                '    strSearchString = "PACK "
                '    Return "SELECT DISTINCT S.ID, S.PackSize + ' ' + S.PackingDescription AS CODE FROM  PackingSize S INNER JOIN PackingUpdateDetail  D ON (D.PackSizeID=S.ID) INNER JOIN  PackingUpdate P ON(P.ID=D.PackingUpdateId) WHERE S.[IsDeleted]=0 AND P.ProductID=" & IntItem.ToString & "  AND ColorID=" & intColor.ToString
            Case Else
                Return ""
        End Select
    End Function

    Private Sub SearchParty_Promoter_Site(ByVal sender As Object)
        Try
            Dim TxtSearch As TextBox = sender

            Dim frmListProducts As New frmProducts
            With frmListProducts

                .Left = fgApprovalItems.Right - .Width - 5
                .Top = fgApprovalItems.Top + 25
                .intSelectedItem = 0
                Dim strSearch As String = ""
                .StrSearchQuery = GetSearchQuery(TxtSearch, strSearch)
                If .StrSearchQuery = String.Empty Then Exit Sub
                .strSearchByCode = strSearch & IIf(sender.Name.ToString.ToUpper.Contains("CODE"), "CODE:", "NAME:")

                .txtProduct.Text = TxtSearch.Text
                .ShowDialog(Me)
                'intSelectedProduct = frmListProducts.intSelectedItem
            End With


            If frmListProducts.intSelectedItem > 0 Then
                With frmListProducts.fgProducts
                    If .CurrentRow.Index >= 0 Then
                        Select Case TxtSearch.Name
                            Case txtPartyCode.Name, txtPartyName.Name
                                intParty = .Item(0, .CurrentRow.Index).Value
                                txtPartyCode.Text = .Item(1, .CurrentRow.Index).Value
                                txtPartyName.Text = .Item(2, .CurrentRow.Index).Value
                                Dim intLocPriceListID As Integer = .Item(4, .CurrentRow.Index).Value
                                If intLocPriceListID > 0 Then
                                    'CmbPriceList.SelectedValue = intLocPriceListID
                                    'cmbPack_SelectionChangeCommitted(Nothing, Nothing)
                                End If
                                If saleType = DGeneral.DocTypes.QUOTATION Then
                                    rTxtAddress.Text = funpriGetTermsAndConditions()
                                Else
                                    rTxtAddress.Text = .Item(3, .CurrentRow.Index).Value
                                End If
                                lblChallanType.Text = .Item(5, .CurrentRow.Index).Value
                                sender.BackColor = Color.White
                                If txtPartyCode.Name = TxtSearch.Name Then
                                    SendKeys.Send("{TAB}")
                                    SendKeys.Send("{TAB}")
                                Else
                                    SendKeys.Send("{TAB}")
                                End If
                            Case txtPromoterCode.Name, txtPromoterName.Name
                                intPromoter = .Item(0, .CurrentRow.Index).Value
                                txtPromoterCode.Text = .Item(1, .CurrentRow.Index).Value
                                txtPromoterName.Text = .Item(2, .CurrentRow.Index).Value
                                sender.BackColor = Color.White
                                If txtPromoterCode.Name = TxtSearch.Name Then
                                    SendKeys.Send("{TAB}")
                                    SendKeys.Send("{TAB}")
                                Else
                                    SendKeys.Send("{TAB}")
                                End If
                            Case txtSiteCode.Name, txtSiteName.Name
                                intSite = .Item(0, .CurrentRow.Index).Value
                                txtSiteCode.Text = .Item(1, .CurrentRow.Index).Value
                                txtSiteName.Text = .Item(2, .CurrentRow.Index).Value
                                Dim intLocPriceListID As Integer = .Item(3, .CurrentRow.Index).Value
                                'If intLocPriceListID > 0 Then
                                '    'CmbPriceList.SelectedValue = intLocPriceListID
                                '    cmbPack_SelectionChangeCommitted(Nothing, Nothing)
                                'End If
                                sender.BackColor = Color.White
                                If txtSiteCode.Name = TxtSearch.Name Then
                                    SendKeys.Send("{TAB}")
                                    SendKeys.Send("{TAB}")
                                Else
                                    SendKeys.Send("{TAB}")
                                End If
                                'Case txtItemCode.Name, txtItemName.Name
                                '    subPriClearItemDtls()
                                '    IntItem = .Item(0, .CurrentRow.Index).Value
                                '    txtItemCode.Text = .Item(1, .CurrentRow.Index).Value
                                '    txtItemName.Text = .Item(2, .CurrentRow.Index).Value
                                '    intTaxID = .Item(3, .CurrentRow.Index).Value
                                '    TaxCode = GetTaxCode(intTaxID)
                                '    SendKeys.Send("{TAB}")
                                'Case txtCLRCODE.Name, txtCLRNAME.Name
                                '    intColor = .Item(0, .CurrentRow.Index).Value
                                '    txtCLRCODE.Text = .Item(1, .CurrentRow.Index).Value
                                '    txtCLRNAME.Text = .Item(2, .CurrentRow.Index).Value
                                '    subPriUpdatePack()
                                '    sender.BackColor = Color.White
                                '    SendKeys.Send("{TAB}")
                                '    'cmbPack.DroppedDown = True
                                'Case txtGoDown.Name ''
                                '    intGodownID = .Item(0, .CurrentRow.Index).Value
                                '    txtGoDown.Text = .Item(2, .CurrentRow.Index).Value
                                '    intPackUpdateDtl_PK = .Item(4, .CurrentRow.Index).Value
                                '    intPackingID = .Item(5, .CurrentRow.Index).Value
                                '    sender.BackColor = Color.White
                                '    SendKeys.Send("{TAB}")
                            Case Else
                                Exit Sub
                        End Select
                    End If
                End With
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "showProductList")
        Finally
            sender.BackColor = Color.White
        End Try
    End Sub
#End Region
    Private Sub SetOtherCharges(Optional ByVal boolLoadforBill As Boolean = False)

        Try
            'SET GRID
            With dgOtherCharges
                .AllowUserToAddRows = False
                .AllowUserToDeleteRows = False
                .AllowUserToOrderColumns = False
                .AllowUserToResizeRows = False
                .RowHeadersVisible = False
                Dim DtbOtherCharges As New DataTable

                If boolLoadforBill = True Then
                    DtbOtherCharges = DSale.GetSaleBillOtherCharges(intVoucherID)
                Else
                    DtbOtherCharges = DSale.GetOtherCharges()

                End If
                Dim pk(0) As System.Data.DataColumn
                pk(0) = DtbOtherCharges.Columns(0)
                DtbOtherCharges.PrimaryKey = pk
                .DataSource = DtbOtherCharges
                .Columns(0).Visible = False  'ID
                .Columns(4).Visible = False  'ISTAXABLE
                .Columns(1).ReadOnly = True 'NAME
                .Columns(3).ReadOnly = True 'Amount
            End With

        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "")
        End Try
    End Sub

    Private Sub CalculateTotalOtherCharges()
        Dim dblOtherCharges As Double = 0.0
        Dim strValue As String
        Try
            With dgOtherCharges
                For intRow As Int16 = 0 To .Rows.Count - 1
                    strValue = .Item(2, intRow).Value
                    .Item(3, intRow).Value = GetOtherCharge(strValue) * IIf(.Item(4, intRow).Value = True, DGeneral.OtherChargTAx, 1)
                    dblOtherCharges = dblOtherCharges + System.Convert.ToDouble(.Item(3, intRow).Value)
                Next
                txtOtherCharges.Text = dblOtherCharges
                txtGrossAmt.Text = System.Convert.ToDouble(txtTotalAmt.Text) + dblOtherCharges
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "")
        End Try
    End Sub

    Private Sub dgOtherCharges_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
        CalculateTotalOtherCharges()
    End Sub
    Private Function GetOtherCharge(strValue As String) As Double
        If strValue.Contains("%") Then
            strValue = strValue.Replace("%", "")
            Dim DblValue As Double = Convert.ToDouble(strValue)
            Dim dblTotalAmt As Double = Convert.ToDouble(txtTotalAmt.Text)
            Return Format((DblValue * dblTotalAmt) / 100, "0.00")
        Else
            Return Convert.ToDouble(strValue)
        End If
    End Function
    Private Sub dgOtherCharges_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If BCommon.ValidateText(txtOtherCharges.Text, e.KeyCode, "NUM", True) = False Then
            If e.KeyCode <> Keys.A% Then
                e.SuppressKeyPress = True
            End If
        End If
    End Sub

#Region "Summary"
    Private Sub LoadPacking()
        dtbPacking = DbHelper.ExecuteDataTable("SELECT Id,PackingContain AS COUNTS,PackingSizeId,PackingCode, PackingDescription AS Pack, 0 AS QTY FROM Packing WHERE IsDeleted=0")
        Dim pk(0) As System.Data.DataColumn
        pk(0) = dtbPacking.Columns(0)
        dtbPacking.PrimaryKey = pk
    End Sub
    Private Sub SubPriUpdateAndDisplaySumary()
        Dim strSmry As StringBuilder = New StringBuilder
        For Each drPack As DataRow In dtbPacking.Rows
            If (drPack.Item(5) > 0) Then
                If drPack.Item("QTY") < drPack.Item("COUNTS") Then
                    strSmry.Append(drPack.Item("PackingCode").ToString & "=" & drPack.Item("QTY").ToString() & "LOOSE")
                ElseIf drPack.Item("COUNTS") = drPack.Item("QTY") Then
                    strSmry.Append(drPack.Item("PackingCode").ToString & "=1" & drPack.Item("Pack").ToString())
                Else
                    Dim Qty As Int16 = drPack.Item("QTY") \ drPack.Item("COUNTS")
                    strSmry.Append(drPack.Item("PackingCode").ToString & "=" & Qty.ToString & drPack.Item("Pack").ToString() & ",")
                    strSmry.Append(drPack.Item("QTY") Mod drPack.Item("COUNTS") & "LOOSE")
                End If
                strSmry.Append(",")
            End If
        Next
        If strSmry.ToString.Length > 0 Then
            lblSummary.Text = strSmry.ToString
        End If
    End Sub

    Private Sub AddToSummary(ByVal locPackID As Int16, ByVal Qty As Integer)
        Try
            Dim PackRow As DataRow = dtbPacking.Rows.Find(locPackID)
            PackRow.Item(5) += Qty
        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "")
        End Try
    End Sub

    Private Sub ClearSummary()
        Try
            For Each drPack As DataRow In dtbPacking.Rows
                drPack.Item(5) = 0
            Next
            lblSummary.Text = "Summary:"
        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "")
        End Try
    End Sub

#End Region

#Region "Discount"
    Private Sub txtDscAmt_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        e.SuppressKeyPress = True
        SendKeys.Send("{TAB}")
    End Sub
    Private Sub txtItemDsc_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.A% Then
        ElseIf BCommon.ValidateText(txtOtherCharges.Text, e.KeyCode, "NUM", True) = False Then
            e.SuppressKeyPress = True
        End If
    End Sub
    'Private Sub txtDisc_Validated(ByVal sender As Object, ByVal e As System.EventArgs)
    '    SubPriCalcItemAmnt()
    'End Sub
    'Private Sub txtItemDsc_TextChanged(sender As Object, e As System.EventArgs) Handles txtDisc.TextChanged
    'End Sub

    Private Function CalculateItemDisc(strValue As String, ByVal dblApplyTo As Double) As Double
        If strValue = String.Empty Then
            Return 0
        ElseIf strValue.Contains("%") Then
            strValue = strValue.Replace("%", "")
            Dim DblValue As Double = Convert.ToDouble(strValue)
            Return (DblValue * dblApplyTo) / 100
        Else
            Return Convert.ToDouble(strValue)
        End If
    End Function
#End Region

#Region "TAX"


    Private Function GetTaxCode(iTaxID As Integer) As String
        Dim objStrctTax As StructTaxDetails = hstTAxes.Item(iTaxID)
        Return objStrctTax.StrTaxValue
    End Function
    Private Function CalculateItemTax(dblAmt As Double, intTaxID As Integer) As Double
        If intTaxID = 0 Then Return 0
        Dim objStrctTax As StructTaxDetails = hstTAxes.Item(intTaxID)
        Dim dblTaxValue As Double
        If Not IsNothing(objStrctTax) Then
            dblTaxValue = objStrctTax.StrTaxValue
            Dim dblTaxAmt As Double
            dblTaxAmt = (dblTaxValue / (dblTaxValue + 100)) * dblAmt
            Return Format(dblTaxAmt, "0.00")
        End If
    End Function

    Private Function funpriGetTermsAndConditions() As String
        Return "User Defined terms and conditions"
    End Function
    Private Sub AddTaxes(ByVal LocTAXID As Int16, ByVal TAXAmt As Double, ByVal TotalAmt As Double)
        Try
            If LocTAXID > 0 Then
                Dim TaxRow As DataRow = dtbTaxes.Rows.Find(LocTAXID)
                TaxRow.Item(2) += TotalAmt - TAXAmt
                TaxRow.Item(3) += TAXAmt
                TaxRow.Item(4) += TotalAmt
                txtTaxableAmt.Text = txtTaxableAmt.Text + (TotalAmt - TAXAmt)
                txtTaxAmt.Text = txtTaxAmt.Text + TAXAmt
                txtTaxTotal.Text = txtTaxTotal.Text + TotalAmt
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "")
        End Try
    End Sub

    Private Sub ClearTaxes()
        Try
            For Each drTax As DataRow In dtbTaxes.Rows
                drTax.Item(2) = 0
                drTax.Item(3) = 0
                drTax.Item(4) = 0
            Next
            txtTaxableAmt.Text = 0
            txtTaxAmt.Text = 0
            txtTaxTotal.Text = 0
        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "")
        End Try
    End Sub

    Private Sub LoadTaxes(Optional ByVal dtbSaleTax As DataTable = Nothing)
        Try
            hstTAxes = New Hashtable
            If IsNothing(dtbSaleTax) Then
                dtbTaxes = DbHelper.ExecuteDataTable("SELECT Id AS TAXID,TaxPercentage AS TAX,'0' AS TAXABLEAMT,'0' AS TAXAMT,'0' AS TOTAL FROM TAX WHERE IsDeleted=0")
            Else
                dtbTaxes = dtbSaleTax
                Dim dblTxbleAmt As Double
                Dim dblTaxAmt As Double
                For Each TaxRow As DataRow In dtbSaleTax.Rows
                    dblTxbleAmt += TaxRow.Item("TAXABLEAMT")
                    dblTaxAmt += TaxRow.Item("TAXAMT")
                Next
                txtTaxableAmt.Text = dblTxbleAmt
                txtTaxAmt.Text = dblTaxAmt
                txtTaxTotal.Text = dblTaxAmt + dblTxbleAmt
            End If
            Dim pk(0) As System.Data.DataColumn
            pk(0) = dtbTaxes.Columns(0)
            dtbTaxes.PrimaryKey = pk
            dgTaxes.DataSource = dtbTaxes
            Dim objStrctTax As StructTaxDetails = New StructTaxDetails
            For Each drTaxRow As DataRow In dtbTaxes.Rows
                With drTaxRow
                    objStrctTax.intTax = .Item(0)
                    objStrctTax.StrTaxValue = .Item(1)
                    objStrctTax.StrTotalTaxableAmt = 0
                    objStrctTax.strTotalTaxApplied = 0
                End With
                hstTAxes.Add(objStrctTax.intTax, objStrctTax)
            Next

        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "")
        End Try
    End Sub
    Private Sub SubPriSetTaxGrid()
        Try
            With dgTaxes
                '.ColumnCount = 5
                .Columns(0).HeaderText = "ID"
                .Columns(0).Visible = False
                .Columns(1).HeaderText = "Tax"
                .Columns(2).HeaderText = "Taxable Amt"
                .Columns(3).HeaderText = "Tax Amt"
                .Columns(4).HeaderText = "Total"
                .Columns(1).Width = .Width / 6
                .Columns(2).Width = .Width / 3.3
                .Columns(3).Width = .Width / 4
                .Columns(4).Width = .Width / 5
                .Columns(1).ReadOnly = True
                .Columns(2).ReadOnly = True
                .Columns(3).ReadOnly = True
                .Columns(4).ReadOnly = True
                txtTaxableAmt.Width = .Width / 3.3
                txtTaxAmt.Width = .Width / 4
                txtTaxTotal.Width = .Width / 5
                .RowHeadersVisible = False
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "")
        End Try
    End Sub
#End Region

#Region "TOTALS"

#End Region

#Region "SEARCH"
    Public SrchStrPOs As String
    Private SrchIntSupplier As Integer
    Private SrchIntItem As Integer
    Private SrchIntColor As Integer
    Private SrchIntGodownID As Integer
    Private SrchIntPackSizeID As Integer
    Private srchIntPartyID As Integer

    Private Sub SrchBtnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SrchBtnClear.Click
        Try
            SrchTxtPartyCode.Text = ""
            SrchtxtPartyName.Text = ""
            SrchtxtPromoterCD.Text = ""

            SrchtxtPromoterName.Text = ""
            SrchTxtItemName.Text = ""
            SrchtxtItemCode.Text = ""
            srchtxtCLRNAME.Text = ""
            SrchtxtGodown.Text = ""
            srchtxtCLRCODE.Text = ""
            srchCmbPack.SelectedIndex = -1
            SrchStrPOs = String.Empty

            SrchIntSupplier = 0
            srchIntPartyID = 0
            SrchIntGodownID = 0
            SrchIntItem = 0
            SrchIntColor = 0
            SrchIntPackSizeID = 0
            SubPriClearUnselectedItems()
        Catch ex As Exception

        End Try
    End Sub
    Private Sub SrchcmbPack_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles srchCmbPack.GotFocus
        sender.DroppedDown = True
    End Sub
    Private Sub SrchcmbPack_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles srchCmbPack.SelectionChangeCommitted

        SrchIntPackSizeID = srchCmbPack.SelectedValue
        '  SubPriGetPrice()
    End Sub



    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearchChallans.Click
        Try
            'Dim strWhereClause As String = String.Empty
            ''IMP commented for challan update.
            'If srchintSupplier > 0 Then
            '    strWhereClause = " AND  P.PromoterID = " & SrchIntSupplier.ToString()
            'End If

            'If srchIntItem > 0 Then
            '    strWhereClause = strWhereClause & "  AND D.ItemID = " & SrchIntItem.ToString()
            'End If

            'If srchintColor > 0 Then
            '    strWhereClause = strWhereClause & " AND  D.CLRCode = " & SrchIntItem.ToString()
            'End If
            'If srchintPackSizeID > 0 Then
            '    strWhereClause = strWhereClause & "  AND D.PackUpdateID = " & SrchIntPackSizeID.ToString()
            'End If
            'If srchchkDate.CheckState = CheckState.Checked Then
            '    strWhereClause = strWhereClause & "  AND P.PurchaseDate  BETWEEN '" & Format(dtpFrom.Value, "MM/dd/yyyy") & " ' AND  '" & Format(DtpTO.Value, "MM/dd/yyyy") & "' "
            'End If
            'If SrchIntGodownID > 0 Then
            '    strWhereClause = strWhereClause & "  AND D.GodwonID = " & SrchIntPackSizeID.ToString()
            'End If

            'If strWhereClause = String.Empty Then
            '    strWhereClause = strWhereClause & "  AND P.PurchaseDate  = '" & Format(Now(), "MM/dd/yyyy") & " ' "
            'End If
            If srchIntPartyID = 0 Then
                MessageBox.Show("PLEASE SELECT THE PARTY!")
                Exit Sub
            End If
            Dim strWhereClause As String = String.Empty
            If SrchlblPartyType.Text = "C" Then
                strWhereClause = " AND  S.Billed = 0 "
            End If

            If SrchIntSupplier > 0 Then
                strWhereClause = " AND  S.PromoterID = " & SrchIntSupplier.ToString()
            End If

            If SrchIntItem > 0 Then
                strWhereClause = strWhereClause & "  AND D.ItemID = " & SrchIntItem.ToString()
            End If

            If SrchIntColor > 0 Then
                strWhereClause = strWhereClause & " AND  D.CLRCode = " & SrchIntColor.ToString()
            End If
            If SrchIntPackSizeID > 0 Then
                strWhereClause = strWhereClause & "  AND D.PackUpdateID = " & SrchIntPackSizeID.ToString()
            End If
            If srchchkDate.CheckState = CheckState.Checked Then
                strWhereClause = strWhereClause & "  AND S.SaleDate  BETWEEN '" & Format(dtpFrom.Value, "MM/dd/yyyy") & " ' AND  '" & Format(DtpTO.Value, "MM/dd/yyyy") & "' "
            End If
            If SrchIntGodownID > 0 Then
                strWhereClause = strWhereClause & "  AND U.GodownID = " & SrchIntGodownID.ToString()
            End If

            'If strWhereClause = String.Empty Then
            '    strWhereClause = strWhereClause & "  AND P.PurchaseDate  = '" & Format(Now(), "MM/dd/yyyy") & " ' "
            'End If

            If strWhereClause <> String.Empty Then

                Dim strSearchQuery As String
                strSearchQuery = "SELECT D.PackUpdateID, D.SaleID, D.ItemID ,0 AS SelectItem, S.SeqSaleID AS VoucherNo,P.PromoterName,S.SaleDate,I.ProductCode,I.ProductName, C.ColorCode,C.ColorDescription AS COLOR,Z.PackSize + ' ' + Z.PackingDescription AS PACK, D.Qty, D.SalePrice, D.DISC_AMT, D.Amount ,D.TaxID,D.TaxAmt,U.PackSizeID,U.PackingID AS PAckingID,D.CLRCODE , ISNULL(P.ID ,0)AS PROMOTERCODE FROM tSale S INNER JOIN tSaleDet D  ON(S.SaleID=D.SaleID) INNER JOIN PARTY T ON( T.ID = S.PartyCode  )INNER JOIN PRODUCT I ON (D.ItemID=I.ID) INNER JOIN PackingUpdateDetail U ON (U.ID=D.PackUpdateID)  INNER  JOIN PackingSize Z ON(U.PackSizeID=Z.ID) INNER JOIN COLOR C ON (C.ID=D.CLRCode)LEFT OUTER JOIN Promoter P ON (S.PromoterID=P.ID) WHERE SaleType = " & System.Convert.ToInt16(DGeneral.DocTypes.CHALLAN).ToString() & strWhereClause

                ''IMP strSearchQuery = "SELECT  DISTINCT P.PurchaseID, 0 AS SelectPO,SeqPurchaseID AS PONumber,S.SupplierName,PurchaseDate AS PO_DATE,P.PromoterID FROM tPurchase P INNER JOIN tPurchaseDet D ON(P.PurchaseID=D.PurchaseID) LEFT OUTER JOIN SUPPLIER S ON (S.ID=P.PromoterID)  WHERE PurchaseType = " & System.Convert.ToInt16(DGeneral.DocTypes.CHALLAN).ToString() & strWhereClause
                'Debug.WriteLine(strSearchQuery)
                subPriFillList(strSearchQuery)
                If srchchkDate.CheckState = CheckState.Checked Then
                    strSearchQuery = " ISNULL(SUM(D.Amount),0) AS AMOUNT FROM tSale S INNER JOIN tSaleDet D  ON(S.SaleID=D.SaleID)  WHERE SaleType = " & System.Convert.ToInt16(DGeneral.DocTypes.CHALLAN).ToString() & "   AND S.SaleDate  BETWEEN '" & Format(dtpFrom.Value, "MM/dd/yyyy") & " ' AND  '" & Format(DtpTO.Value, "MM/dd/yyyy") & "' "
                    txtDateTotal.Text = DbHelper.ExecuteScalar(strSearchQuery)
                Else
                    txtDateTotal.Text = "0.00"
                End If
            Else
                MessageBox.Show(" Please select search criteria")
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub SetApprovalGrid()
        With Me.fgApprovalItems


            .RowHeadersVisible = False
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            .AllowUserToResizeRows = False
            .AllowUserToAddRows = False
        End With
    End Sub
    Private Sub SubPriClearUnselectedItems()
        ''CLEAR unSelected Rows
        With Me.fgApprovalItems
            '  fgApprovalItems.Rows.Count
            Dim iCount As Integer = 0
            While .Rows.Count > 0 And iCount < .Rows.Count

                If (.Item(SarchAprvlColumns.PackUpdateID, iCount).Value = 0) Then
                    Exit While
                End If
                If (.Item(SarchAprvlColumns.SelectItem, iCount).Value = False) Then
                    .Rows.RemoveAt(iCount)
                Else
                    iCount += 1
                End If
            End While
            CalculateToTals()
        End With
    End Sub
    Private Sub subPriFillList(ByVal strQuery As String)
        Try
            Dim ItemReader As SqlDataReader = DbHelper.ExecuteDataReader(strQuery)
            With Me.fgApprovalItems
                If ItemReader.IsClosed Then Exit Sub
                Dim intCount As Integer = 0
                '.ColumnCount = 12


                SubPriClearUnselectedItems()
                .Refresh()

                BtLoading = True
                While ItemReader.Read()
                    ''CHECK NEW ITEM ALREADY EXIST IN SELECTED LIST 
                    For iCount As Integer = 0 To .Rows.Count - 1
                        If .Item(SarchAprvlColumns.PackUpdateID, iCount).Value = ItemReader(SarchAprvlColumns.PackUpdateID) And .Item(SarchAprvlColumns.SaleID, iCount).Value = ItemReader(SarchAprvlColumns.SaleID) And .Item(SarchAprvlColumns.ItemID, iCount).Value = ItemReader(SarchAprvlColumns.ItemID) Then
                            Continue While
                        End If
                    Next
                    intCount = .RowCount
                    .RowCount += 1

                    .Item(SarchAprvlColumns.PackUpdateID, intCount).Value = ItemReader(SarchAprvlColumns.PackUpdateID)  'PackupdateID
                    .Item(SarchAprvlColumns.SaleID, intCount).Value = ItemReader(SarchAprvlColumns.SaleID)    'SaleID, 
                    .Item(SarchAprvlColumns.ItemID, intCount).Value = ItemReader(SarchAprvlColumns.ItemID)   'ItemID
                    .Item(SarchAprvlColumns.SelectItem, intCount).Value = ItemReader(SarchAprvlColumns.SelectItem) 'Selection

                    .Item(SarchAprvlColumns.VoucherNo, intCount).Value = ItemReader(SarchAprvlColumns.VoucherNo)
                    .Item(SarchAprvlColumns.PromoterName, intCount).Value = ItemReader(SarchAprvlColumns.PromoterName)
                    .Item(SarchAprvlColumns.SaleDate, intCount).Value = ItemReader(SarchAprvlColumns.SaleDate)
                    .Item(SarchAprvlColumns.ProductCode, intCount).Value = ItemReader(SarchAprvlColumns.ProductCode)
                    .Item(SarchAprvlColumns.ProductName, intCount).Value = ItemReader(SarchAprvlColumns.ProductName)
                    .Item(SarchAprvlColumns.COLORCODE, intCount).Value = ItemReader(SarchAprvlColumns.COLORCODE)
                    .Item(SarchAprvlColumns.COLORNAME, intCount).Value = ItemReader(SarchAprvlColumns.COLORNAME)
                    .Item(SarchAprvlColumns.PACK, intCount).Value = ItemReader(SarchAprvlColumns.PACK)
                    .Item(SarchAprvlColumns.Qty, intCount).Value = ItemReader(SarchAprvlColumns.Qty)
                    .Item(SarchAprvlColumns.SalePrice, intCount).Value = ItemReader(SarchAprvlColumns.SalePrice)
                    .Item(SarchAprvlColumns.Disc, intCount).Value = ItemReader(SarchAprvlColumns.Disc)
                    .Item(SarchAprvlColumns.Amount, intCount).Value = ItemReader(SarchAprvlColumns.Amount)
                    .Item(SarchAprvlColumns.TaxID, intCount).Value = ItemReader(SarchAprvlColumns.TaxID)
                    .Item(SarchAprvlColumns.TAXAMT, intCount).Value = ItemReader(SarchAprvlColumns.TAXAMT)
                    .Item(SarchAprvlColumns.CLRID, intCount).Value = ItemReader(SarchAprvlColumns.CLRID)
                    .Item(SarchAprvlColumns.PACK_SIZE_ID, intCount).Value = ItemReader(SarchAprvlColumns.PACK_SIZE_ID)
                    .Item(SarchAprvlColumns.PACKINGID, intCount).Value = ItemReader(SarchAprvlColumns.PACKINGID)
                    .Item(SarchAprvlColumns.PromoterCode, intCount).Value = ItemReader(SarchAprvlColumns.PromoterCode)
                    '.Item(13, intCount).Value = ItemReader(13)
                    '.Item(14, intCount).Value = ItemReader(14)
                    'For intColcount As Integer = 0 To ItemReader.FieldCount - 1
                    '    .Item(intColcount, intCount).Value = ItemReader(intColcount)
                    'Next
                    intCount += 1
                End While
                CalculateToTals()
            End With
            ItemReader.Close()
        Catch ex As Exception

        Finally
            BtLoading = False
        End Try

    End Sub
    Private Enum SarchAprvlColumns
        PackUpdateID = 0
        SaleID = 1
        ItemID = 2
        SelectItem = 3
        VoucherNo = 4
        PromoterName = 5
        SaleDate = 6
        ProductCode = 7
        ProductName = 8
        COLORCODE = 9
        COLORNAME = 10
        PACK = 11
        Qty = 12
        SalePrice = 13
        Disc = 14
        Amount = 15
        TaxID = 16
        TAXAMT = 17
        PACK_SIZE_ID = 18
        PACKINGID = 19
        CLRID = 20
        PromoterCode = 21
    End Enum

    Private Sub chkSelectAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSelectAll.CheckedChanged
        Dim CurrentStatus As Boolean = chkSelectAll.CheckState
        For intcnt As Int16 = 0 To fgApprovalItems.Rows.Count - 1
            fgApprovalItems.Item(SarchAprvlColumns.SelectItem, intcnt).Value = CurrentStatus
        Next
        CalculateToTals()
    End Sub

    Private Sub SrchtxtPartyCode_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles SrchtxtPromoterCD.Enter, SrchtxtPromoterName.Enter, SrchtxtItemCode.Enter, SrchTxtItemName.Enter, srchtxtCLRCODE.Enter, srchtxtCLRNAME.Enter, SrchtxtGodown.Enter, SrchTxtPartyCode.Enter, SrchtxtPartyName.Enter
        If BtLoading Then Exit Sub
        If sender.Text.ToString = String.Empty Then
            sender.BackColor = Color.LightBlue
            SearchParty_Promoter_ForAprovals(sender)
        End If
    End Sub
    Private Sub SrchtxtItemCode_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles SrchtxtItemCode.KeyDown, SrchTxtItemName.KeyDown, srchtxtCLRCODE.KeyDown, srchtxtCLRNAME.KeyDown, SrchtxtGodown.KeyDown, SrchTxtPartyCode.KeyDown, SrchtxtPartyName.KeyDown

        If (srchCmbPack.Name <> sender.Name) AndAlso e.KeyCode = Keys.Back Then
            e.SuppressKeyPress = True
            SearchParty_Promoter_ForAprovals(sender)
        ElseIf e.KeyCode = Keys.Enter Then
            SendKeys.Send("{Tab}")
        ElseIf e.KeyCode = Keys.Tab Then
            'ElseIf (cmbPack.Name <> sender.Name) Then
            '    e.SuppressKeyPress = True
        End If
    End Sub

    Private Function GetChallanSearchQuery(ByVal Sender As Object, ByRef strSearchString As String) As String
        Select Case Sender.Name
            Case SrchTxtPartyCode.Name, SrchtxtPartyName.Name
                strSearchString = "PARTY "
                Return "SELECT P.Id,P.PartyCode AS CODE ,P.PartyName  AS NAME, P.Address +', PIN:' + CONVERT(VARCHAR (6),P.PinCode) +', ' + C.CityName  AS PartyAddrs,ISNULL(PriceListId,0) AS PriceListId,P.ChallanType  FROM [NGSPaintsDB].[dbo].Party P INNER JOIN City	C ON (P.CityID= C.ID) WHERE P.IsDeleted=0 " '& IIf(saleType = DGeneral.DocTypes.INVOICE, " AND P.ChallanType='C'", "")
            Case SrchtxtPromoterName.Name, SrchtxtPromoterCD.Name
                strSearchString = "PROMOTER "
                Return "SELECT DISTINCT P.Id,P.PromoterCode AS CODE ,P.PromoterName AS NAME FROM [NGSPaintsDB].[dbo].promoter P INNER JOIN TSale T ON(P.ID =T.PromoterID AND T.Billed = 0 AND T.SaleType = " & DGeneral.DocTypes.CHALLAN & " ) WHERE P.IsDeleted=0 "

                'Case txtSiteCode.Name, txtSiteName.Name
                '    strSearchString = "SITE "
                '    Return " SELECT [Id],[SiteCode]AS CODE ,[SiteName] AS NAME,ISNULL(PriceListId,0) AS PriceListId  FROM [NGSPaintsDB].[dbo].[Site] WHERE [IsDeleted]=0" & IIf(intParty > 0, " AND PartyID=" & intParty.ToString, "")
                '    'Case txtPrice.Name 'PRICE LIST
                '    'CmbPriceList.Focus()    Return "SELECT [ID],[PRICELISTCODE]AS CODE ,[PRICELISTNAME] AS NAME  FROM [NGSPAINTSDB].[DBO].[PRICELIST] WHERE [ISDELETED]=0"
            Case SrchtxtItemCode.Name, SrchTxtItemName.Name
                strSearchString = "PRODUCT "
                Return "SELECT DISTINCT  P.[Id] AS ItemID,P.[ProductCode] AS Code,P.[ProductName] AS Name, P.TAXID,P.IsColor FROM [NGSPaintsDB].[dbo].Product P INNER JOIN tSaleDet D ON(P.ID = D.ItemID AND Qty > RetQty)INNER JOIN TSale T ON(D.SaleID = T.SaleID AND T.Billed =0 AND T.SaleType =  " & DGeneral.DocTypes.CHALLAN & " )  WHERE ISDeleted=0 ORDER BY NAME"
            Case srchtxtCLRCODE.Name, srchtxtCLRNAME.Name
                strSearchString = "COLOR "
                Return "SELECT DISTINCT C.[Id],C.[ColorCode] AS CODE ,C.[ColorDescription] AS NAME  FROM [Color] AS C  INNER JOIN  PackingUpdateDetail  D ON (D.ColorID=C.ID) INNER JOIN  PackingUpdate P ON(P.ID=D.PackingUpdateId) WHERE C.[IsDeleted]=0 " & IIf(SrchIntItem = 0, "", " AND P.ProductID=" & SrchIntItem.ToString)
            Case SrchtxtGodown.Name
                'If SrchIntItem = 0 OrElse SrchIntColor = 0 OrElse SrchIntPackSizeID = 0 Then
                '    Return ""
                'End If
                strSearchString = "GODOWN "
                Return "SELECT DISTINCT (C.[Id]),C.[GoDownCode] AS CODE ,C.[GodownName] AS NAME ,Stock, D.ID AS PackUpdateID,D.PackingID FROM PackingUpdateDetail D INNER JOIN [GoDown] C ON (D.GoDownID=C.ID " & IIf(SrchIntColor = 0, "", " AND D.ColorID=" & SrchIntColor) & ")  INNER JOIN  PackingUpdate P ON(P.ID=D.PackingUpdateId" & IIf(SrchIntItem = 0, "", "  AND P.ProductID=" & SrchIntItem) & ")  WHERE Stock > 0" & IIf(SrchIntPackSizeID = 0, "", " AND D.PackSizeID=" & SrchIntPackSizeID.ToString())
            Case srchCmbPack.Name
                'If SrchIntItem = 0 OrElse SrchIntColor = 0 Then
                '    Return ""
                'End If
                strSearchString = "PACK "
                Return "SELECT DISTINCT S.ID, S.PackSize + ' ' + S.PackingDescription AS CODE FROM  PackingSize S INNER JOIN PackingUpdateDetail  D ON (D.PackSizeID=S.ID) INNER JOIN  PackingUpdate P ON(P.ID=D.PackingUpdateId) WHERE S.[IsDeleted]=0" & IIf(SrchIntItem = 0, "", " AND P.ProductID=" & SrchIntItem.ToString()) & IIf(SrchIntColor = 0, "", "  AND ColorID=" & SrchIntColor.ToString())
            Case Else
                Return ""
        End Select
    End Function

    'Private Sub fgApprovalItems_BindingContextChanged(sender As Object, e As System.EventArgs) Handles fgApprovalItems.BindingContextChanged
    '    Try
    '        If BtLoading Then Return
    '        BtLoading = True
    '        With fgApprovalItems

    '            If (.CurrentCell.ColumnIndex = SarchAprvlColumns.SelectItem) Then

    '                AddTaxes(.Item(SarchAprvlColumns.TaxID, .CurrentCell.RowIndex).Value, .Item(SarchAprvlColumns.TAXAMT, .CurrentCell.RowIndex).Value, .Item(SarchAprvlColumns.Amount, .CurrentCell.RowIndex).Value)
    '                CalculateToTals()
    '                '.CurrentCell = .Item(SarchAprvlColumns.Qty, .CurrentCell.RowIndex)
    '            End If
    '        End With
    '    Catch ex As Exception

    '    Finally
    '        BtLoading = False
    '    End Try
    'End Sub

    Private Sub fgApprovalItems_CellBeginEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles fgApprovalItems.CellBeginEdit
        With fgApprovalItems
            If (.CurrentCell.ColumnIndex = SarchAprvlColumns.Qty Or .CurrentCell.ColumnIndex = SarchAprvlColumns.Disc) And (.Item(SarchAprvlColumns.SelectItem, .CurrentCell.RowIndex).Value = False) Then
                e.Cancel = True
            End If
        End With
    End Sub



    Private Sub fgApprovalItems_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles fgApprovalItems.CellEndEdit
        Try
            If BtLoading Then Return
            BtLoading = True
            With fgApprovalItems

                If Not IsNumeric(.Item(SarchAprvlColumns.Qty, .CurrentCell.RowIndex).Value.ToString) Then
                    .Item(SarchAprvlColumns.Qty, .CurrentCell.RowIndex).Value = 0
                End If
                If Not IsNumeric(.Item(SarchAprvlColumns.Disc, .CurrentCell.RowIndex).Value.ToString.Replace(".", "")) Then
                    .Item(SarchAprvlColumns.Disc, .CurrentCell.RowIndex).Value = "0.00"
                End If
                Dim x As Double = System.Convert.ToDouble(fgApprovalItems.Item(SarchAprvlColumns.Qty, fgApprovalItems.CurrentCell.RowIndex).Value) * fgApprovalItems.Item(SarchAprvlColumns.SalePrice, fgApprovalItems.CurrentCell.RowIndex).Value

                .Item(SarchAprvlColumns.Amount, .CurrentCell.RowIndex).Value = x '- System.Convert.ToInt16(.Item(SarchAprvlColumns.Disc, .CurrentCell.RowIndex).Value)
                .Item(SarchAprvlColumns.TAXAMT, .CurrentCell.RowIndex).Value = CalculateItemTax(.Item(SarchAprvlColumns.Amount, .CurrentCell.RowIndex).Value, .Item(SarchAprvlColumns.TaxID, .CurrentCell.RowIndex).Value)
                AddTaxes(.Item(SarchAprvlColumns.TaxID, .CurrentCell.RowIndex).Value, .Item(SarchAprvlColumns.TAXAMT, .CurrentCell.RowIndex).Value, .Item(SarchAprvlColumns.Amount, .CurrentCell.RowIndex).Value)
                CalculateToTals()
            End With
        Catch ex As Exception

        Finally
            BtLoading = False
        End Try
    End Sub
    Private Sub CalculateToTals()
        Try
            With fgApprovalItems
                Dim dblTotalItems As Double
                Dim dblTotalSelecteds As Double
                Dim dblTotalUnSelected As Double

                For intCount As Integer = 0 To .Rows.Count - 1
                    If .Item(SarchAprvlColumns.SelectItem, intCount).Value = False Then
                        dblTotalUnSelected += .Item(SarchAprvlColumns.Amount, intCount).Value
                    Else
                        dblTotalSelecteds += .Item(SarchAprvlColumns.Amount, intCount).Value
                    End If
                Next

                dblTotalItems = dblTotalUnSelected + dblTotalSelecteds
                txtSelectedTotal.Text = dblTotalSelecteds.ToString()
                txtUnSelected.Text = dblTotalUnSelected.ToString()
                txtSearchedTotal.Text = dblTotalItems.ToString()
                If System.Convert.ToDecimal(txtDateTotal.Text) > 0 Then
                    txtSearchedMinusSelected.Text = System.Convert.ToDecimal(txtDateTotal.Text) - System.Convert.ToDecimal(txtSelectedTotal.Text)
                End If

            End With

        Catch ex As Exception

        End Try
        CalculateApprovalAmt()
    End Sub
    Private Sub CalculateApprovalAmt()
        Dim intCount As Integer
        Dim TotAmount As Double = 0      '
        Dim TotalDiscAmt As Double = 0
        Try
            With fgApprovalItems

                ClearTaxes()
                ClearSummary()
                For intCount = 0 To .Rows.Count - 1
                    If IsNothing(.Item(SarchAprvlColumns.Amount, intCount).Value) OrElse Trim(.Item(SarchAprvlColumns.Amount, intCount).Value) = "" Then Exit For
                    If .Item(SarchAprvlColumns.SelectItem, intCount).Value = True Then
                        TotAmount = TotAmount + .Item(SarchAprvlColumns.Amount, intCount).Value
                        'TotalDiscAmt = TotalDiscAmt + .Item(SarchAprvlColumns.DISC_AMT, intCount).Value
                        AddTaxes(.Item(SarchAprvlColumns.TaxID, intCount).Value, .Item(SarchAprvlColumns.TAXAMT, intCount).Value, .Item(SarchAprvlColumns.Amount, intCount).Value)
                        AddToSummary(.Item(SarchAprvlColumns.PACKINGID, intCount).Value, .Item(SarchAprvlColumns.Qty, intCount).Value)
                    End If
                Next
                txtTotalAmt.Text = Format(TotAmount, "0.00")
                txtTotalDisc.Text = Format(TotalDiscAmt, "0.00")
                CalculateTotalOtherCharges()
                SubPriUpdateAndDisplaySumary()
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "CalculateAmt")
        End Try
    End Sub
    Private Sub SearchParty_Promoter_ForAprovals(ByVal sender As Object)
        Try
            Dim TxtSearch As TextBox = sender

            Dim frmListProducts As New frmProducts
            With frmListProducts

                .Left = fgApprovalItems.Right - .Width - 5
                .Top = fgApprovalItems.Top + 25
                .intSelectedItem = 0
                Dim strSearch As String = ""
                .StrSearchQuery = GetChallanSearchQuery(TxtSearch, strSearch)
                If .StrSearchQuery = String.Empty Then Exit Sub
                .strSearchByCode = strSearch & IIf(sender.Name.ToString.ToUpper.Contains("CODE"), "CODE:", "NAME:")

                .txtProduct.Text = TxtSearch.Text
                .ShowDialog(Me)
                'intSelectedProduct = frmListProducts.intSelectedItem
            End With


            If frmListProducts.intSelectedItem > 0 Then
                With frmListProducts.fgProducts
                    If .CurrentRow.Index >= 0 Then
                        Select Case TxtSearch.Name
                            Case SrchTxtPartyCode.Name, SrchtxtPartyName.Name
                                srchIntPartyID = .Item(0, .CurrentRow.Index).Value
                                SrchTxtPartyCode.Text = .Item(1, .CurrentRow.Index).Value
                                SrchtxtPartyName.Text = .Item(2, .CurrentRow.Index).Value
                                ' Dim intLocPriceListID As Integer = .Item(4, .CurrentRow.Index).Value
                                'If intLocPriceListID > 0 Then
                                '    CmbPriceList.SelectedValue = intLocPriceListID
                                '    cmbPack_SelectionChangeCommitted(Nothing, Nothing)
                                'End If
                                rTxtAddress.Text = .Item(3, .CurrentRow.Index).Value
                                'If PurchaseType = DGeneral.DocTypes.QUOTATION Then
                                '    rTxtAddress.Text = funpriGetTermsAndConditions()
                                'Else
                                '    rTxtAddress.Text = .Item(3, .CurrentRow.Index).Value
                                'End If
                                SrchlblPartyType.Text = .Item(5, .CurrentRow.Index).Value
                                sender.BackColor = Color.White
                                If SrchtxtPartyName.Name = TxtSearch.Name Then
                                    SendKeys.Send("{TAB}")
                                    SendKeys.Send("{TAB}")
                                Else
                                    SendKeys.Send("{TAB}")
                                End If
                            Case txtPromoterCode.Name, txtPromoterName.Name
                                SrchIntSupplier = .Item(0, .CurrentRow.Index).Value
                                SrchtxtPromoterCD.Text = .Item(1, .CurrentRow.Index).Value
                                SrchtxtPromoterName.Text = .Item(2, .CurrentRow.Index).Value
                                sender.BackColor = Color.White
                                SendKeys.Send("{TAB}")
                                'Case txtSiteCode.Name, txtSiteName.Name
                                '    intSite = .Item(0, .CurrentRow.Index).Value
                                '    txtSiteCode.Text = .Item(1, .CurrentRow.Index).Value
                                '    txtSiteName.Text = .Item(2, .CurrentRow.Index).Value
                                '    Dim intLocPriceListID As Integer = .Item(3, .CurrentRow.Index).Value
                                '    If intLocPriceListID > 0 Then
                                '        'CmbPriceList.SelectedValue = intLocPriceListID
                                '        cmbPack_SelectionChangeCommitted(Nothing, Nothing)
                                '    End If
                                '    sender.BackColor = Color.White
                                '    SendKeys.Send("{TAB}")
                            Case SrchtxtItemCode.Name, SrchTxtItemName.Name
                                'subPriClearItemDtls()
                                SrchIntItem = .Item(0, .CurrentRow.Index).Value
                                SrchtxtItemCode.Text = .Item(1, .CurrentRow.Index).Value
                                SrchTxtItemName.Text = .Item(2, .CurrentRow.Index).Value
                                'intTaxID = .Item(3, .CurrentRow.Index).Value
                                'TaxCode = GetTaxCode(intTaxID)
                                SendKeys.Send("{TAB}")
                            Case srchtxtCLRCODE.Name, srchtxtCLRNAME.Name
                                SrchIntColor = .Item(0, .CurrentRow.Index).Value
                                srchtxtCLRCODE.Text = .Item(1, .CurrentRow.Index).Value
                                srchtxtCLRNAME.Text = .Item(2, .CurrentRow.Index).Value
                                subPriUpdateAprrovalPack()
                                sender.BackColor = Color.White
                                SendKeys.Send("{TAB}")
                                'cmbPack.DroppedDown = True
                            Case SrchtxtGodown.Name ''
                                SrchIntGodownID = .Item(0, .CurrentRow.Index).Value
                                SrchtxtGodown.Text = .Item(2, .CurrentRow.Index).Value
                                'sintPackUpdateDtl_PK = .Item(4, .CurrentRow.Index).Value
                                'srchintPackingID = .Item(5, .CurrentRow.Index).Value
                                sender.BackColor = Color.White
                                SendKeys.Send("{TAB}")
                            Case Else
                                Exit Sub
                        End Select
                    End If
                End With
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "showProductList")
        Finally
            sender.BackColor = Color.White
        End Try
    End Sub
    Private Sub subPriUpdateAprrovalPack()
        BtLoading = True
        Dim strPackQury As String = GetChallanSearchQuery(srchCmbPack, "")
        Dim dtbpack As DataTable = DbHelper.ExecuteDataTable(strPackQury)
        Dim pk(0) As System.Data.DataColumn
        pk(0) = dtbpack.Columns(0)
        dtbpack.PrimaryKey = pk
        srchCmbPack.DataSource = dtbpack
        srchCmbPack.ValueMember = "ID"
        srchCmbPack.DisplayMember = "CODE"
        srchCmbPack.SelectedIndex = -1
        BtLoading = False

    End Sub
#End Region

    Private Sub fgApprovalItems_CellValidated(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles fgApprovalItems.CellValidated
        Try
            If BtLoading Then Return
            BtLoading = True
            With fgApprovalItems

                If (.CurrentCell.ColumnIndex = SarchAprvlColumns.SelectItem) Then

                    AddTaxes(.Item(SarchAprvlColumns.TaxID, .CurrentCell.RowIndex).Value, .Item(SarchAprvlColumns.TAXAMT, .CurrentCell.RowIndex).Value, .Item(SarchAprvlColumns.Amount, .CurrentCell.RowIndex).Value)
                    CalculateToTals()
                    '.CurrentCell = .Item(SarchAprvlColumns.Qty, .CurrentCell.RowIndex)
                End If
            End With
        Catch ex As Exception

        Finally
            BtLoading = False
        End Try
    End Sub

    Private Sub fgApprovalItems_CellValueChanged(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles fgApprovalItems.CellValueChanged
        Try
            If BtLoading Then Return
            With fgApprovalItems
                If .CurrentCell.ColumnIndex = SarchAprvlColumns.SelectItem Then
                    CalculateAmt()
                End If
            End With

        Catch ex As Exception

        End Try
    End Sub
    Private Sub rdbCashMemo_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rdbCashMemo.CheckedChanged
        SubPriSetSeqID()
    End Sub
#Region "COMMENTED CODE"

#Region "GRID SETTINGS"

    'Private Sub SetGrids()
    '    Try
    '        'ITEM DISPLAY GRID
    '        SetApprovalGrid()
    '        With fgItems
    '            .Left = pnlMain.Left + 5
    '            .Width = pnlMain.Width - 1
    '            .RowHeadersVisible = False
    '            .AllowUserToAddRows = False
    '            .AllowUserToDeleteRows = False
    '            .AllowUserToOrderColumns = False
    '            .AllowUserToResizeRows = False
    '            .AllowUserToResizeColumns = False

    '            '.AllowDelete = True
    '            .RowCount = intGrdCntRows
    '            '.Cols.Fixed = 0
    '            .ColumnCount = intTotalColumns
    '            .ScrollBars = ScrollBars.Both
    '            '.AllowAddNew = False
    '            .SelectionMode = DataGridViewSelectionMode.FullRowSelect




    '            'HIDDEN COLUMNS
    '            For intcnt As Integer = 0 To .ColumnCount - 1

    '                .Columns(intcnt).Visible = False
    '                .Columns(intcnt).ReadOnly = True
    '                .Columns(intcnt).Width = 0

    '                '  .Width = 0
    '            Next
    '            Call SetGridCaptions()
    '            .Columns(ItemGridColumns.CODE).Visible = True
    '            .Columns(ItemGridColumns.ITEMNAME).Visible = True
    '            .Columns(ItemGridColumns.CLRCODE).Visible = True
    '            .Columns(ItemGridColumns.CLRDESC).Visible = True
    '            .Columns(ItemGridColumns.PACK).Visible = True
    '            .Columns(ItemGridColumns.GODOWN).Visible = True
    '            .Columns(ItemGridColumns.QTY).Visible = True

    '            .Columns(ItemGridColumns.DISC).Visible = True
    '            .Columns(ItemGridColumns.DISC_AMT).Visible = True
    '            .Columns(ItemGridColumns.RATE).Visible = True
    '            .Columns(ItemGridColumns.AMOUNT).Visible = True
    '            .Columns(ItemGridColumns.TAXCODE).Visible = True
    '        End With
    '    Catch ex As Exception
    '        BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "SetGrids")
    '    End Try
    'End Sub

    'Private Sub SetColumnWidths()
    '    Dim intColWidth As Integer = fgItems.Width / (11 * 3) '10 number of visible columns *3
    '    Try
    '        With fgItems

    '            .Columns(ItemGridColumns.CODE).Width = intColWidth * 2
    '            .Columns(ItemGridColumns.ITEMNAME).Width = intColWidth * 7
    '            .Columns(ItemGridColumns.CLRCODE).Width = intColWidth * 2
    '            .Columns(ItemGridColumns.CLRDESC).Width = intColWidth * 3
    '            .Columns(ItemGridColumns.PACK).Width = intColWidth * 3
    '            .Columns(ItemGridColumns.GODOWN).Width = intColWidth * 3
    '            .Columns(ItemGridColumns.QTY).Width = intColWidth * 2

    '            .Columns(ItemGridColumns.DISC).Width = intColWidth * 2
    '            .Columns(ItemGridColumns.DISC_AMT).Width = intColWidth * 2
    '            .Columns(ItemGridColumns.RATE).Width = intColWidth * 2
    '            .Columns(ItemGridColumns.AMOUNT).Width = intColWidth * 3
    '            .Columns(ItemGridColumns.TAXCODE).Width = intColWidth * 2


    '            txtItemCode.Width = .Columns(ItemGridColumns.CODE).Width
    '            txtItemName.Width = .Columns(ItemGridColumns.ITEMNAME).Width
    '            txtCLRCODE.Width = .Columns(ItemGridColumns.CLRCODE).Width
    '            txtCLRNAME.Width = .Columns(ItemGridColumns.CLRDESC).Width
    '            cmbPack.Width = .Columns(ItemGridColumns.PACK).Width
    '            txtGoDown.Width = .Columns(ItemGridColumns.GODOWN).Width
    '            txtQty.Width = .Columns(ItemGridColumns.QTY).Width
    '            txtDisc.Width = .Columns(ItemGridColumns.DISC).Width
    '            txtDscAmt.Width = .Columns(ItemGridColumns.DISC_AMT).Width
    '            txtPrice.Width = .Columns(ItemGridColumns.RATE).Width
    '            txtItemAmount.Width = .Columns(ItemGridColumns.AMOUNT).Width


    '            txtItemCode.Left = .Left
    '            txtItemName.Left = txtItemCode.Left + txtItemCode.Width
    '            txtCLRCODE.Left = txtItemName.Left + txtItemName.Width
    '            txtCLRNAME.Left = txtCLRCODE.Left + txtCLRCODE.Width
    '            cmbPack.Left = txtCLRNAME.Left + txtCLRNAME.Width
    '            txtGoDown.Left = cmbPack.Left + cmbPack.Width

    '            txtQty.Left = txtGoDown.Left + txtGoDown.Width
    '            txtPrice.Left = txtQty.Left + txtQty.Width

    '            txtDisc.Left = txtPrice.Left + txtPrice.Width
    '            txtDscAmt.Left = txtDisc.Left + txtDisc.Width
    '            txtItemAmount.Left = txtDscAmt.Left + txtDscAmt.Width
    '        End With

    '    Catch ex As Exception
    '        BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "SetColumnWidths")
    '    End Try
    'End Sub

    'Private Sub SetGridCaptions()
    '    Try
    '        With fgItems
    '            'SET COLUMN NAMES
    '            '.Columns(0).Name = "ITEMID"
    '            '.Columns(1).Name = "CODE"
    '            '.Columns(2).Name = "ITEMNAME"
    '            '.Columns(3).Name = "CLRCODE"
    '            '.Columns(4).Name = "CLRDESC"
    '            '.Columns(5).Name = "PACK"
    '            '.Columns(6).Name = "GODOWN"
    '            '.Columns(7).Name = "QTY"
    '            '.Columns(8).Name = "RATE"
    '            '.Columns(9).Name = "DISCVALUE"
    '            '.Columns(10).Name = "DISCAMT"
    '            '.Columns(11).Name = "AMOUNT"
    '            '.Columns(12).Name = "STATUS"

    '            '.Columns(13).Name = "CLRID"
    '            '.Columns(14).Name = "GODOWNID"

    '            '.Columns(15).Name = "STOCKDETAILS"
    '            '.Columns(16).Name = "TAXID"

    '            '.Columns(17).Name = "PACKID"
    '            '.Columns(18).Name = "PACKUPDATEID"
    '            '.Columns(19).Name = "TAXAMT"
    '            '.Columns(20).Name = "PACKSIZEID"
    '            '.Columns(19).Name = "TAXAMT"


    '            'SET COLUMN CAPTIONS
    '            .Columns(ItemGridColumns.ITEMID).HeaderText = "ITEMID"
    '            .Columns(ItemGridColumns.CODE).HeaderText = "CODE"
    '            .Columns(ItemGridColumns.ITEMNAME).HeaderText = "ITEMNAME"
    '            .Columns(ItemGridColumns.CLRCODE).HeaderText = "COLOR CODE"
    '            .Columns(ItemGridColumns.CLRDESC).HeaderText = "COLOR DESC"
    '            .Columns(ItemGridColumns.PACK).HeaderText = "PACK"
    '            .Columns(ItemGridColumns.GODOWN).HeaderText = "GODOWN"


    '            .Columns(ItemGridColumns.QTY).HeaderText = "QTY"
    '            .Columns(ItemGridColumns.RATE).HeaderText = "PRICE"
    '            .Columns(ItemGridColumns.DISC).HeaderText = "DISC"
    '            .Columns(ItemGridColumns.DISC_AMT).HeaderText = "DISC AMT"
    '            .Columns(ItemGridColumns.AMOUNT).HeaderText = "AMOUNT"
    '            .Columns(ItemGridColumns.STATUS).HeaderText = "STATUS"
    '            .Columns(ItemGridColumns.CLRID).HeaderText = "COLORID"
    '            .Columns(ItemGridColumns.GODOWNID).HeaderText = "GODOWNID"
    '            .Columns(ItemGridColumns.TAXID).HeaderText = "TAX ID"
    '            .Columns(ItemGridColumns.TAXAMT).HeaderText = "TAX APPLIED"
    '            .Columns(ItemGridColumns.TAXCODE).HeaderText = "TAX"
    '            .Columns(ItemGridColumns.PACK_SIZE_ID).HeaderText = "PACK ID"
    '            .Columns(ItemGridColumns.PACKUPDATEID_PK).HeaderText = "PACKUPDATEID"
    '        End With
    '    Catch ex As Exception
    '        BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "SetGridCaption")
    '    End Try
    'End Sub

#End Region '"GRID SETTINGS"
#Region "Button EVENTS"
    'Private Sub subPriDisablePArtyOptions(ByVal bnlVisible)
    '    txtPartyCode.Enabled = bnlVisible
    '    txtPartyName.Enabled = bnlVisible
    '    txtPromoterCode.Enabled = bnlVisible
    '    txtPromoterName.Enabled = bnlVisible
    '    txtSiteCode.Enabled = bnlVisible
    '    txtSiteName.Enabled = bnlVisible
    '    lblChallanType.Visible = bnlVisible
    'End Sub

    'Private Sub BtnCash_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    Try

    '        subPriDisablePArtyOptions(True)
    '        saleType = DGeneral.DocTypes.CASH_MEMO
    '        lblShipingAddress.Text = "Shipping Adress:"
    '        SubPriSetSeqID()
    '        lblChallanType.Visible = False
    '    Catch ex As Exception
    '        BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "Button1_Click")
    '    End Try
    'End Sub

    'Private Sub BtnChallan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    Try
    '        Me.BtnCash.BackColor = Color.WhiteSmoke
    '        Me.BtnChallan.BackColor = Color.SteelBlue
    '        Me.BtnInvoice.BackColor = Color.WhiteSmoke
    '        Me.BtnApprove.BackColor = Color.WhiteSmoke
    '        Me.BtnQuote.BackColor = Color.WhiteSmoke
    '        saleType = DGeneral.DocTypes.CHALLAN
    '        lblShipingAddress.Text = "Shipping Adress:"
    '        subPriDisablePArtyOptions(True)
    '        SubPriSetSeqID()
    '        lblChallanType.Visible = True
    '    Catch ex As Exception
    '        BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "BtnChallan_Click")
    '    End Try
    'End Sub

    'Private Sub BtnInvoice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    Try
    '        Me.BtnCash.BackColor = Color.WhiteSmoke
    '        Me.BtnChallan.BackColor = Color.WhiteSmoke
    '        Me.BtnInvoice.BackColor = Color.SteelBlue
    '        Me.BtnApprove.BackColor = Color.WhiteSmoke
    '        Me.BtnQuote.BackColor = Color.WhiteSmoke
    '        saleType = DGeneral.DocTypes.INVOICE
    '        lblShipingAddress.Text = "Shipping Adress:"
    '        subPriDisablePArtyOptions(True)
    '        SubPriSetSeqID()
    '        lblChallanType.Visible = True
    '    Catch ex As Exception
    '        BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "BtnInvoice_Click")
    '    End Try
    'End Sub

    'Private Sub BtnApprove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    Try
    '        Me.BtnCash.BackColor = Color.WhiteSmoke
    '        Me.BtnChallan.BackColor = Color.WhiteSmoke
    '        Me.BtnInvoice.BackColor = Color.WhiteSmoke
    '        Me.BtnApprove.BackColor = Color.SteelBlue
    '        Me.BtnQuote.BackColor = Color.WhiteSmoke
    '        saleType = DGeneral.DocTypes.CHALLAN
    '        lblShipingAddress.Text = "Shipping Adress:"
    '        subPriDisablePArtyOptions(False)
    '        SubPriSetSeqID()
    '        lblChallanType.Visible = True
    '    Catch ex As Exception
    '        BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "BtnApprove_Click")
    '    End Try
    'End Sub

    'Private Sub BtnQuote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    Try
    '        Me.BtnCash.BackColor = Color.WhiteSmoke
    '        Me.BtnChallan.BackColor = Color.WhiteSmoke
    '        Me.BtnInvoice.BackColor = Color.WhiteSmoke
    '        Me.BtnApprove.BackColor = Color.WhiteSmoke
    '        Me.BtnQuote.BackColor = Color.SteelBlue
    '        saleType = DGeneral.DocTypes.QUOTATION
    '        subPriDisablePArtyOptions(True)
    '        'lblShipingAddress.Text = "Shipping Adress:"
    '        lblShipingAddress.Text = "Terms and Conditions:"
    '        intPriceList = ConfigurationManager.AppSettings("DPL")
    '        CmbPriceList.SelectedValue = intPriceList
    '        SubPriSetSeqID()
    '        lblChallanType.Visible = True
    '    Catch ex As Exception
    '        BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "Button5_Click")
    '    End Try
    'End Sub

#End Region
    'Private Sub SetTabIndexes()
    '    Try
    '        LblTotalOtherCharges.TabIndex = fgItems.TabIndex + 1
    '        txtOtherCharges.TabIndex = LblTotalOtherCharges.TabIndex + 1
    '        pnlAmount.TabIndex = txtOtherCharges.TabIndex + 1
    '    Catch ex As Exception
    '        BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "SetTabIndexes")
    '    End Try
    'End Sub
    'Private Sub subPriUpdatePack()
    '    BtLoading = True
    '    Dim strPackQury As String = GetSearchQuery(cmbPack, "")
    '    Dim dtbpack As DataTable = DbHelper.ExecuteDataTable(strPackQury)
    '    Dim pk(0) As System.Data.DataColumn
    '    pk(0) = dtbpack.Columns(0)
    '    dtbpack.PrimaryKey = pk
    '    cmbPack.DataSource = dtbpack
    '    cmbPack.ValueMember = "ID"
    '    cmbPack.DisplayMember = "CODE"
    '    cmbPack.SelectedIndex = -1
    '    BtLoading = False

    'End Sub

    'Private Sub cmbPack_GotFocus(sender As Object, e As System.EventArgs)
    '    sender.DroppedDown = True
    'End Sub
    'Private Sub cmbPack_SelectionChangeCommitted(sender As Object, e As System.EventArgs)
    '    If BtLoading Then Exit Sub
    '    intPackSizeID = cmbPack.SelectedValue
    '    SubPriGetPrice()
    'End Sub
    'Private Sub txtPrice_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    '    e.SuppressKeyPress = True  'INSTEAD OF DISABLE CONTROL(IT AFFECTS THE UI) MANUAY KEYS ARE SUPPRESSED SO USER CANT EDIT  THE VALUE. TEXT BOX LOOKS ENABLE BUT NOT
    '    SendKeys.Send("{TAB}")
    'End Sub

    'Private Sub txtQty_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    '    If BCommon.ValidateText(txtQty.Text.Trim, e.KeyCode, "NUM", False) = False Then
    '        e.SuppressKeyPress = True
    '    ElseIf e.KeyCode = Keys.Enter Then
    '        SendKeys.Send("{TAB}")
    '    End If
    'End Sub

    'Private Sub txtPrice_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    SubPriCalcItemAmnt()
    'End Sub

    'Private Sub SubPriCalcItemAmnt()
    '    Try
    '        If Me.txtPrice.Text.ToString() <> String.Empty AndAlso Me.txtQty.Text.ToString() <> String.Empty Then
    '            Dim dblNetAmt As Double = Convert.ToDouble(Me.txtPrice.Text.ToString()) * Convert.ToDouble(Me.txtQty.Text.ToString())
    '            Dim dblDisc As Double = 0
    '            If txtDisc.Text.Trim <> "" Then
    '                dblDisc = CalculateItemDisc(txtDisc.Text, dblNetAmt)

    '            End If
    '            txtDscAmt.Text = Format(dblDisc, "0.00")


    '            txtItemAmount.Text = dblNetAmt - dblDisc
    '        Else
    '            txtItemAmount.Text = "0"
    '            txtDscAmt.Text = "0"
    '        End If
    '    Catch ex As Exception
    '        BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "")
    '        txtItemAmount.Text = 0
    '    End Try
    'End Sub
    'Private Sub txtAmount_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    '    If e.KeyCode = Keys.Enter Then
    '        SetItemToGrid()
    '    End If
    '    e.SuppressKeyPress = True
    'End Sub
    'Private Sub SetItemToGrid()
    '    Try
    '        If funPriIsValidItem() Then
    '            subPriSetItemInGrid()
    '            subPriClearItemDtls()
    '            txtItemCode.Focus()
    '        End If
    '    Catch ex As Exception
    '        BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "")
    '    End Try
    'End Sub
    'Private Sub txtAmount_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)
    '    SetItemToGrid()
    'End Sub
    'Private Function funPriIsValidItem() As Boolean
    '    Try
    '        Dim BlnResult As Boolean = True
    '        If IntItem = 0 Then
    '            txtItemCode.Focus()
    '            BlnResult = False
    '        ElseIf intColor = 0 Then
    '            txtCLRCODE.Focus()
    '            BlnResult = False
    '            'ElseIf intGodownID = 0 Then
    '            '    txtGoDown.Focus()
    '            '    BlnResult = False
    '        ElseIf txtQty.Text = String.Empty OrElse System.Convert.ToInt64(txtQty.Text) <= 0 Then
    '            txtQty.Focus()
    '            BlnResult = False
    '        ElseIf txtPrice.Text.Trim = String.Empty OrElse System.Convert.ToDouble(txtPrice.Text) <= 0 Then
    '            txtPrice.Focus()
    '            Return False
    '        End If
    '        Return BlnResult
    '    Catch ex As Exception
    '        BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "")
    '        Return False
    '    End Try
    'End Function

    'Private Sub subPriSetItemInGrid()
    '    Try


    '        With fgItems

    '            Dim intCurrentRow As Integer
    '            For intCurrentRow = 0 To .Rows.Count - 1
    '                If IsNothing(.Item(ItemGridColumns.ITEMID, intCurrentRow).Value) OrElse .Item(ItemGridColumns.ITEMID, intCurrentRow).Value.ToString() = String.Empty Then
    '                    Exit For
    '                End If
    '            Next
    '            If intCurrentRow = .Rows.Count Then
    '                .Rows.Add(1)
    '            End If

    '            .Item(ItemGridColumns.CODE, intCurrentRow).Value = txtItemCode.Text

    '            .Item(ItemGridColumns.ITEMNAME, intCurrentRow).Value = txtItemName.Text
    '            .Item(ItemGridColumns.CLRCODE, intCurrentRow).Value = txtCLRCODE.Text
    '            .Item(ItemGridColumns.CLRDESC, intCurrentRow).Value = txtCLRNAME.Text
    '            .Item(ItemGridColumns.PACK, intCurrentRow).Value = cmbPack.Text
    '            .Item(ItemGridColumns.GODOWN, intCurrentRow).Value = txtGoDown.Text
    '            .Item(ItemGridColumns.RATE, intCurrentRow).Value = txtPrice.Text
    '            .Item(ItemGridColumns.QTY, intCurrentRow).Value = txtQty.Text
    '            .Item(ItemGridColumns.AMOUNT, intCurrentRow).Value = txtItemAmount.Text
    '            .Item(ItemGridColumns.DISC, intCurrentRow).Value = txtDisc.Text
    '            .Item(ItemGridColumns.DISC_AMT, intCurrentRow).Value = txtDscAmt.Text

    '            .Item(ItemGridColumns.ITEMID, intCurrentRow).Value = IntItem
    '            .Item(ItemGridColumns.GODOWNID, intCurrentRow).Value = intGodownID
    '            .Item(ItemGridColumns.CLRID, intCurrentRow).Value = intColor
    '            .Item(ItemGridColumns.PACK_SIZE_ID, intCurrentRow).Value = intPackSizeID
    '            .Item(ItemGridColumns.PACKUPDATEID_PK, intCurrentRow).Value = intPackUpdateDtl_PK
    '            .Item(ItemGridColumns.PACKINGID, intCurrentRow).Value = intPackingID ''used for summary
    '            .Item(ItemGridColumns.TAXID, intCurrentRow).Value = intTaxID
    '            .Item(ItemGridColumns.TAXCODE, intCurrentRow).Value = TaxCode
    '            .Item(ItemGridColumns.TAXAMT, intCurrentRow).Value = CalculateItemTax(txtItemAmount.Text)
    '            '.Item(ItemGridColumns.GODOW, intCurrentRow).Value = intGodownID
    '            intCurrentRow = 0
    '            CalculateAmt()
    '            CalculateTotalOtherCharges()

    '        End With
    '    Catch ex As Exception
    '        BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "")
    '    Finally

    '    End Try
    'End Sub
    'Sub subPriClearItemDtls()
    '    Try
    '        txtItemCode.Text = String.Empty
    '        txtItemName.Text = String.Empty
    '        txtCLRCODE.Text = String.Empty
    '        txtCLRNAME.Text = String.Empty
    '        cmbPack.SelectedIndex = -1
    '        txtGoDown.Text = String.Empty
    '        txtQty.Text = String.Empty

    '        txtItemAmount.Text = String.Empty
    '        txtPrice.Text = String.Empty
    '        txtDisc.Text = String.Empty
    '        txtDscAmt.Text = String.Empty

    '        intPackSizeID = 0
    '        intColor = 0
    '        IntItem = 0
    '        intGodownID = 0
    '        intPackUpdateDtl_PK = 0
    '        intPackingID = 0
    '        intTaxID = 0
    '        TaxCode = ""
    '    Catch ex As Exception
    '        BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "")
    '    End Try
    'End Sub
    'Private Sub SubPriGetPrice()
    '    Try
    '        txtPrice.Text = DbHelper.ExecuteScalar("SELECT Rate FROM PriceListDetail WHERE PriceListID=" & intPriceList & " AND ProductID=" & IntItem & " AND PAckingID=" & intPackSizeID)
    '    Catch ex As Exception
    '        BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "")
    '    End Try
    'End Sub

    'Private Sub CmbPriceList_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    '    'If e.KeyCode = Keys.Enter Then
    '    '    SendKeys.Send("{Tab}")
    '    'End If
    'End Sub

    'Private Sub CmbPriceList_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs)
    '    If BtLoading Then Exit Sub
    '    If Not CmbPriceList.SelectedValue = Nothing Then
    '        intPriceList = CmbPriceList.SelectedValue
    '    End If
    'End Sub

    'Private Sub fgItems_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
    '    With fgItems
    '        If Not IsNothing(.CurrentRow) AndAlso Not IsNothing(.Item(ItemGridColumns.AMOUNT, .CurrentRow.Index).Value) Then
    '            Dim intCurrentRow As Integer = .CurrentRow.Index
    '            IntItem = .Item(ItemGridColumns.ITEMID, intCurrentRow).Value
    '            intPackSizeID = .Item(ItemGridColumns.PACK_SIZE_ID, intCurrentRow).Value
    '            txtQty.Text = .Item(ItemGridColumns.QTY, intCurrentRow).Value
    '            intColor = .Item(ItemGridColumns.CLRID, intCurrentRow).Value
    '            intPackUpdateDtl_PK = .Item(ItemGridColumns.PACKUPDATEID_PK, intCurrentRow).Value
    '            intPackingID = .Item(ItemGridColumns.PACKINGID, intCurrentRow).Value
    '            intTaxID = .Item(ItemGridColumns.TAXID, intCurrentRow).Value
    '            TaxCode = .Item(ItemGridColumns.TAXCODE, intCurrentRow).Value

    '            txtItemCode.Text = .Item(ItemGridColumns.CODE, intCurrentRow).Value
    '            txtItemName.Text = .Item(ItemGridColumns.ITEMNAME, intCurrentRow).Value
    '            txtCLRCODE.Text = .Item(ItemGridColumns.CLRCODE, intCurrentRow).Value
    '            txtCLRNAME.Text = .Item(ItemGridColumns.CLRDESC, intCurrentRow).Value
    '            txtGoDown.Text = .Item(ItemGridColumns.GODOWN, intCurrentRow).Value
    '            txtPrice.Text = .Item(ItemGridColumns.RATE, intCurrentRow).Value
    '            txtDisc.Text = .Item(ItemGridColumns.DISC, intCurrentRow).Value
    '            txtDscAmt.Text = .Item(ItemGridColumns.DISC_AMT, intCurrentRow).Value
    '            txtItemAmount.Text = .Item(ItemGridColumns.AMOUNT, intCurrentRow).Value

    '            subPriUpdatePack()
    '            cmbPack.SelectedValue = intPackSizeID
    '            .Rows.Remove(.CurrentRow)
    '        End If
    '    End With
    'End Sub

    'Private Sub fgItems_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    '    If e.KeyCode = Keys.Enter Then
    '        fgItems_DoubleClick(Nothing, Nothing)
    '    ElseIf e.KeyCode = Keys.Delete Then
    '        If MessageBox.Show("Do you want to delete record?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = vbYes Then
    '            SubPriDeleteRecord()
    '        End If
    '    End If
    'End Sub
    'Private Sub SubPriDeleteRecord()
    '    Try
    '        With fgItems
    '            If Not IsNothing(.Item(ItemGridColumns.AMOUNT, .CurrentRow.Index).Value) Then
    '                .Rows.Remove(.CurrentRow)
    '            End If
    '        End With

    '    Catch ex As Exception
    '        BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "")
    '    Finally
    '        CalculateAmt()
    '        CalculateTotalOtherCharges()
    '    End Try
    'End Sub
#Region "SEARCH SALE"
    '    'Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs)
    '    '    SearchVoucher()
    '    'End Sub
    '    'Private Sub txtSearch_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs)
    '    '    If e.KeyCode = Keys.Enter Then
    '    '        SearchVoucher()
    '    '        Exit Sub
    '    '    End If
    '    '    If BCommon.ValidateText(txtSearch.Text.Trim, e.KeyCode, "NUM", False) = False Then
    '    '        e.SuppressKeyPress = True
    '    '    End If
    '    'End Sub

    '    'Private Sub SearchVoucher()
    '    '    Try
    '    '        If txtSearch.Text.Trim() = String.Empty OrElse System.Convert.ToInt16(txtSearch.Text) = 0 Then
    '    '            MessageBox.Show("Please Enter the ID", "NiranjanPaints", MessageBoxButtons.OK, MessageBoxIcon.Error)
    '    '        Else
    '    '            Dim intSeqVoucherID As Int64 = System.Convert.ToInt16(txtSearch.Text)

    '    '            intVoucherID = DbHelper.ExecuteScalar("SELECT SaleID FROM tSale WHERE SeqSaleID =" & intSeqVoucherID.ToString() & " AND SaleType=" & System.Convert.ToInt16(saleType).ToString())
    '    '            If (intVoucherID > 0) Then
    '    '                subPriFillDetails()
    '    '            Else
    '    '                MessageBox.Show("Record not found!!!", "NiranjanPaints", MessageBoxButtons.OK, MessageBoxIcon.Error)
    '    '            End If
    '    '        End If

    '    '    Catch ex As Exception
    '    '        BCommon.ErrorHandler(ex.ToString, Me.ProductName, Me.Name, "")
    '    '    End Try
    '    'End Sub
    '    Private Sub subPriFillDetails()
    '        Dim intTempVoucherID As Integer = intVoucherID
    '        Call ClearData()
    '        intVoucherID = intTempVoucherID
    '        Dim DtbSale As DataTable = DSale.GetSaleBill(intVoucherID)
    '        Dim dtbSaleItems As DataTable = DSale.GetSaleBillItems(intVoucherID)
    '        Dim dtblocTaxes As DataTable = DSale.GetSaleTaxes(intVoucherID)
    '        'Dim dtbOtherChrgs As DataTable = 


    '        With DtbSale.Rows(0)
    '            'S.SeqSaleID,S.PartyCode AS PartyID,P.PartyCode,P.PartyName, S.SaleDate,S.PromoterID,R.PromoterName,R.PromoterCode,S.SiteID,S.Amount,S.OtherCharges,S.TotalTaxes,
    '            'S.Discount, S.GrossAmount, S.ShipingAddress, S.SaleType, PriceListID, T.SiteName, T.SiteCode
    '            saleType = .Item("SaleType")
    '            'Select Case saleType
    '            '    Case DGeneral.DocTypes.APPROVAL
    '            '        BtnApprove_Click(Nothing, Nothing)
    '            '    Case DGeneral.DocTypes.CASH_MEMO
    '            '        BtnCash_Click(Nothing, Nothing)
    '            '    Case DGeneral.DocTypes.CHALLAN
    '            '        BtnChallan_Click(Nothing, Nothing)
    '            '    Case DGeneral.DocTypes.INVOICE
    '            '        BtnInvoice_Click(Nothing, Nothing)
    '            '    Case DGeneral.DocTypes.QUOTATION
    '            '        BtnQuote_Click(Nothing, Nothing)
    '            'End Select

    '            txtVoucher.Text = .Item("SeqSaleID")
    '            txtPartyCode.Text = .Item("PartyCode")
    '            txtPartyName.Text = .Item("PartyName")
    '            txtPromoterCode.Text = .Item("PromoterCode")
    '            txtPromoterName.Text = .Item("PromoterName")
    '            txtTotalAmt.Text = .Item("Amount")
    '            txtGrossAmt.Text = .Item("GrossAmount")
    '            txtOtherCharges.Text = .Item("OtherCharges")
    '            txtSiteCode.Text = .Item("SiteCode")
    '            txtSiteName.Text = .Item("SiteName")
    '            rTxtAddress.Text = .Item("ShipingAddress")
    '            intParty = .Item("PartyID")
    '            intPromoter = .Item("PromoterID")
    '            intSite = .Item("SiteID")
    '            intPriceList = .Item("PriceListID")
    '            'CmbPriceList.SelectedValue = intPriceList
    '            '.Text = .Item("")

    '            '''''-------------------------Item Details
    '        End With
    '        ''''''Fill Sale Details
    '        'S.ItemID,P.ProductName,P.ProductCode,S.CLRcode AS CLRID,C.ColorCode,C.ColorDescription, S.PackUpdateID,S.SalePrice,S.Qty,S.Amount,S.SchemeDiscount SELECT S.ItemID,
    '        'P.ProductName,P.ProductCode,S.CLRcode AS CLRID,C.ColorCode,C.ColorDescription, S.PackUpdateID,S.SalePrice,S.Qty,S.Amount,S.SchemeDiscount, PCK 
    '        Dim intCurrentRow As Integer = 0
    '        For Each SaleITem As DataRow In dtbSaleItems.Rows

    '            If fgItems.Rows.Count <= intCurrentRow Then
    '                fgItems.Rows.Add(intGrdCntRows)
    '            End If
    '            fgItems.Item(ItemGridColumns.CODE, intCurrentRow).Value = SaleITem.Item("ProductCode") 'txtItemCode.Text

    '            fgItems.Item(ItemGridColumns.ITEMNAME, intCurrentRow).Value = SaleITem.Item("ProductName") ' txtItemName.Text
    '            fgItems.Item(ItemGridColumns.CLRCODE, intCurrentRow).Value = SaleITem.Item("ColorCode") 'txtCLRCODE.Text
    '            fgItems.Item(ItemGridColumns.CLRDESC, intCurrentRow).Value = SaleITem.Item("ColorDescription") 'txtCLRNAME.Text
    '            fgItems.Item(ItemGridColumns.PACK, intCurrentRow).Value = SaleITem.Item("PCK") 'cmbPack.Text
    '            fgItems.Item(ItemGridColumns.GODOWN, intCurrentRow).Value = SaleITem.Item("GodownName") 'txtGoDown.Text
    '            fgItems.Item(ItemGridColumns.RATE, intCurrentRow).Value = SaleITem.Item("SalePrice") 'txtPrice.Text
    '            fgItems.Item(ItemGridColumns.QTY, intCurrentRow).Value = SaleITem.Item("Qty") 'txtQty.Text
    '            fgItems.Item(ItemGridColumns.AMOUNT, intCurrentRow).Value = SaleITem.Item("Amount") 'txtItemAmount.Text
    '            fgItems.Item(ItemGridColumns.DISC, intCurrentRow).Value = SaleITem.Item("DISC")
    '            fgItems.Item(ItemGridColumns.DISC_AMT, intCurrentRow).Value = SaleITem.Item("DISC_AMT")

    '            fgItems.Item(ItemGridColumns.ITEMID, intCurrentRow).Value = SaleITem.Item("ItemID") 'IntItem
    '            fgItems.Item(ItemGridColumns.GODOWNID, intCurrentRow).Value = SaleITem.Item("GodownId") 'intGodownID
    '            fgItems.Item(ItemGridColumns.CLRID, intCurrentRow).Value = SaleITem.Item("CLRID") 'intColor
    '            fgItems.Item(ItemGridColumns.PACK_SIZE_ID, intCurrentRow).Value = SaleITem.Item("PackSizeID") 'intPack
    '            fgItems.Item(ItemGridColumns.PACKUPDATEID_PK, intCurrentRow).Value = SaleITem.Item("PackUpdateIDPK") 'intPackingUpdateID
    '            fgItems.Item(ItemGridColumns.PACKINGID, intCurrentRow).Value = SaleITem.Item("PackingID") ''FOR SUMARY DETAILS
    '            fgItems.Item(ItemGridColumns.TAXID, intCurrentRow).Value = SaleITem.Item("TAXID")
    '            fgItems.Item(ItemGridColumns.TAXAMT, intCurrentRow).Value = SaleITem.Item("TAXAMT")
    '            fgItems.Item(ItemGridColumns.TAXCODE, intCurrentRow).Value = GetTaxCode(SaleITem.Item("TAXID"))
    '            intCurrentRow += 1
    '        Next
    '        'intCurrentRow = 0
    '        ''''''Fill Other Charges
    '        LoadTaxes(dtblocTaxes)
    '        SetOtherCharges(True)
    '    End Sub

#End Region
#End Region


    Private Sub TextBox_GotFocus(sender As Object, e As System.EventArgs) Handles txtGrossAmt.GotFocus, SrchtxtPromoterCD.GotFocus, SrchtxtPromoterName.GotFocus, srchtxtCLRCODE.GotFocus, srchtxtCLRNAME.GotFocus, SrchtxtItemCode.GotFocus, SrchTxtItemName.GotFocus, SrchtxtGodown.GotFocus, txtSearchedMinusSelected.GotFocus, txtDateTotal.GotFocus, txtUnSelected.GotFocus, txtUnSelected.GotFocus, txtSelectedTotal.GotFocus, txtTotalAmt.GotFocus, txtOtherCharges.GotFocus, txtTotalDisc.GotFocus
        Dim txtobject As TextBox = sender
        txtobject.BackColor = Color.LightBlue
    End Sub

    Private Sub TextBox_LostFocus(sender As Object, e As System.EventArgs) Handles txtGrossAmt.LostFocus, SrchtxtPromoterCD.LostFocus, SrchtxtPromoterName.LostFocus, srchtxtCLRCODE.LostFocus, srchtxtCLRNAME.LostFocus, SrchtxtItemCode.LostFocus, SrchTxtItemName.LostFocus, SrchtxtGodown.LostFocus, txtSearchedMinusSelected.LostFocus, txtDateTotal.LostFocus, txtUnSelected.LostFocus, txtUnSelected.LostFocus, txtSelectedTotal.LostFocus, txtTotalAmt.LostFocus, txtOtherCharges.LostFocus, txtTotalDisc.LostFocus
        Dim txtobject As TextBox = sender
        txtobject.BackColor = Color.White
    End Sub

    Private Sub txtPartyCode_Leave(sender As Object, e As System.EventArgs) Handles txtPartyCode.Leave, txtPartyName.Leave
        If sender.Text.trim = String.Empty Then
            intParty = 0
        End If
    End Sub
    Private Sub txtSite_Leave(sender As Object, e As System.EventArgs) Handles txtSiteCode.Leave, txtSiteName.Leave
        If sender.Text.trim = String.Empty Then
            intSite = 0
        End If
    End Sub
  
    Private Sub txtPromoter_Leave(sender As Object, e As System.EventArgs) Handles txtPromoterCode.Leave, txtPromoterName.Leave
        If sender.Text.trim = String.Empty Then
            intPromoter = 0
        End If
    End Sub
    Private Sub dtpSaleDate_CloseUp(sender As System.Object, e As System.EventArgs) Handles dtpSaleDate.CloseUp

        intBlankID = D_EOD.FunPubGetNextBlankEntry(System.Convert.ToInt32(saleType), dtpSaleDate.Value.Date)

        If intBlankID > 0 Then
            strSeqID = DbHelper.ExecuteScalar("Select BlankID FROM T_BlankEntry WHERE SeqID = " & intBlankID.ToString())
            txtVoucher.Text = strSeqID
        Else
            If MessageBox.Show("No blank entries found for selected date,Do you want to select other date?", "NP", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then

                SubPriSelectDate()
            End If
        End If
    End Sub
    Private Sub SubPriSelectDate()
        txtVoucher.Text = ""
        intBlankID = 0
        strSeqID = ""
        dtpSaleDate.Focus()
        SendKeys.Send("{F4}")

    End Sub
End Class
